
; Asar directives
hirom
org $C30000
table ff6_bank_c3.tbl,rtl
incsrc ff6_bank_c3_defs.asm


; Entry points
C30000:  JMP C3001B      ; Invoke desired field menu
C30003:  JMP C3A445      ; Decode joypads for battle
C30006:  JMP C3A424      ; Establish autofire delays
C30009:  JMP C3A47A      ; Decode joypads for field
C3000C:  JMP C313C8      ; Advance play time
C3000F:  JMP C314FE      ; Load save data on defeat
C30012:  JMP C3C51C      ; Show an ending cinematic
C30015:  JMP C396D2      ; Optimize member's gear
C30018:  JMP C3D57F      ; Load map for final land

; Invoke requested field menu
C3001B:  JSR C300C0      ; Reroute NMI, etc.
         LDA $0200       ; Menu to open
         PHA             ; Memorize it
         PHA             ; Do it again
         CMP #$02        ; Load menu?
         BNE C3003F      ; Branch if not
         JSR C3709B      ; Reset game data
C3003F:  JSR C300F8      ; Load GFX, colors
         TDC             ; Clear A
         PLA             ; Menu to open
         ASL A           ; Double it
         TAX             ; Index it
         JSR (C300E6,X)  ; Handle menu
         LDA #$8F        ; BRT: 15 and OFF
         STA $2100       ; Disable screen
         STZ $4200       ; Disable INT, pad

; Fork: Rename Card
C3006F:  PLA             ; In main menu?
         BNE C30057      ; Branch if not
         LDA $0205       ; Rename request?
         BPL C300BF      ; Return if not
         INC $0200       ; Menu: Actor naming
         LDA $0201       ; Area permissions
         PHA             ; Preserve them
         LDY $0206       ; Actor's address
         STY $0201       ; Set as target's
         JSL C30000      ; Call naming menu
         PLA             ; Area permissions
         STA $0201       ; Restore them
         STZ $0200       ; Menu: Main
         BRA C3001B      ; Open main menu

; Fork: Leaving Load menu
C30057:  CMP #$02        ; In Load menu?
         BNE C300BF      ; Return if not
         LDA $1D4E       ; Game options
         AND #$20        ; Stereo sound?
         BEQ C300BF      ; Return if so
         LDA $0205       ; Start new game?
         BPL C300BF      ; Return if so
         JSR C33E3F      ; Adjust volume
C300BF:  RTL

; Jump table for invoking a field menu
C300E6:  dw C3011A       ; Main menu
         dw C301B3       ; Actor naming
         dw C3016F       ; Load menu
         dw C30121       ; Shop
         dw C3012B       ; Lineup
         dw C30000       ; Unused...
         dw C30000       ; SwdTech naming...
         dw C30152       ; Colosseum
         dw C30166       ; Final battle lineup

; Reroute interrupt vectors, set WRAM bank
C300C0:  LDX #C31387     ; C3/1387
         STX $1501       ; Set NMI LBs
         LDX #C313C6     ; C3/13C6
         STX $1505       ; Set IRQ LBs
         LDA #$C3        ; Bank: C3
         STA $1503       ; Set NMI HB
         STA $1507       ; Set IRQ HB
C300C_:  LDA #$7E        ; Bank: 7E
         STA $2183       ; Set WRAM HB
         RTS

; Load menu graphics and colors; initialize general variables
C300F8:  JSR C368FA      ; Reset variables
         JSR C36915      ; Get party info
         JSL $D4CD48     ; Init registers
         JSL C3A424      ; Set autofire delays
         TDC             ; Gradient set: 0
         JSL $D4CA1D     ; Create gradient
         JSR C369DC      ; Queue Win1 HDMA
         JSR C31114      ; Reset OAM, queue
         JSR C36A87      ; Load nonskin GFX
         JMP C33A87      ; Load skin visuals

; Invoke main menu
C3011A:  LDA #$04        ; C3/1A8A
         BRA C301B8      ; Enter menu loop

; Invoke shop menu
C30121:  LDA #$24        ; C3/B466
         BRA C301B_      ; Enter menu loop

; Invoke Lineup menu
C3012B:  LDA #$69        ; C3/70E7
         BRA C301B_      ; Enter menu loop

; Invoke Colosseum menu
C30152:  LDA #$31        ; C3/ACAA
         BRA C301B_      ; Enter menu loop

; Invoke final battle lineup
C30166:  LDA #$45        ; C3/A9F8
         BRA C301B_      ; Enter menu loop

; Invoke Load menu
C3016F:  JSL C3A424      ; Set autofire delays
         PEA $0030       ; Push 00 and 30
         PLB             ; Set DB to 30
         INC $7FF1       ; RNG index in SRAM
         PHP             ; Save PS

; Fork: Validate SRAM markers, delete files on failure
C37023:  JSR srm_mrk     ; Square's marker
.loop    CMP $7FF8,X     ; In current file?
         BEQ C3705_      ; Okay if so
         DEX             ; File index -1
         DEX             ; File index -1
         BPL .loop       ; Loop till < 0
         LDX #$1FFE      ; SRAM index
;        TDC             ; Clear A
C37055:  STZ $6000,X     ; Clear SRAM data
         DEX             ; SRAM index -1
         DEX             ; SRAM index -1
         BPL C37055      ; Loop till < 0
         PLP             ; 8-bit A
         PLB             ; Set DB to 0

; Fork: Force new game
C3019F:  JSR C32A0A      ; Reset time, etc.
         BRA C301D8      ; Exit menu

; Fork: Open Load menu
C3705_:  PLP             ; 8-bit A
         PLB             ; Set DB to 0
         LDX #$0110      ; Song: The Prelude
         JSR C30ED6      ; Play song
         JSL C31514      ; New game mode: No
         LDA #$20        ; C3/1D03
         BRA C301B8      ; Enter menu loop

; Invoke actor naming menu
C301B3:  LDA #$5D        ; C3/652D
C301B_:  PHA             ; Save init cmd
         JSR C33F99      ; Apply user font
         PLA             ; Init command
C301B8:  STA $26         ; Next: Init menu

; Main loop for field menus
C301BA:  JSR C31412      ; Refresh screen
C301BD:  TDC             ; Clear A
         LDA $26         ; Menu command
         BMI C301D8      ; If exit to map
         ASL A           ; Double it
         TAX             ; Index it
         JSR (C301DB,X)  ; Handle menu cmd
         JSR C311B0      ; Handle anim queue
         JSR C3134D      ; Update screen/pad
         JSR C302DB      ; Check event timer
         BRA C301BD      ; Loop
C301D8:  STZ $43         ; Disable HDMA
         RTS

; Jump table for the above
C301DB:  dw C31D71       ; 00: Fade out screen
         dw C31D7E       ; 01: Fade in screen
         dw C31D8B       ; 02: Wait while menu fades in or out
         dw C31D94       ; 03: Exit main menu's portrait area
         dw C31A8A       ; 04: Initialize main menu
         dw C31DA4       ; 05: Sustain main menu
         dw C31DE8       ; 06: Target one actor in main menu
         dw C38983       ; 07: Sustain non-shifted gear data menu
         dw C31EF7       ; 08: Handle Item menu choice 1
         dw C32779       ; 09: Handle Item menu choice 2
         dw C31FF4       ; 0A: Sustain Skills menu
         dw C31C46       ; 0B: Initialize Status menu
         dw C321F5       ; 0C: Sustain Status menu
         dw C31C7D       ; 0D: Initialize Config menu
         dw C33A00       ; 0E: Scroll to Config page 2
         dw C322C5       ; 0F: Sustain Config menu
         dw C33A21       ; 10: Scroll to Config page 1
         dw C324A9       ; 11: Switch members in Order menu
         dw C31B35       ; 12: Return from item usage menu
         dw C31CA0       ; 13: Initialize Save menu
         dw C32540       ; 14: Sustain Save menu
         dw C31CE3       ; 15: Initialize save confirmation
         dw C3259D       ; 16: Sustain save confirmation
         dw C325F4       ; 17: Handle Item menu options
         dw C32741       ; 18: Sustain rare item menu
         dw C31B5B       ; 19: Initialize Skills menu
         dw C327E2       ; 1A: Sustain Magic menu
         dw C3288A       ; 1B: Sustain Lore menu
         dw C328AA       ; 1C: Sustain Dance menu
         dw C328BA       ; 1D: Sustain Rage menu
         dw C3ACDC       ; 1E: Sustain Colosseum item menu
         dw C363A7       ; 1F: Sustain shifted Status menu
         dw C31D03       ; 20: Initialize Load menu
         dw C329C2       ; 21: Sustain Load menu
         dw C31D51       ; 22: Initialize load confirmation
         dw C32A2A       ; 23: Sustain load confirmation
         dw C3B466       ; 24: Initialize shop menu
         dw C3B49B       ; 25: Sustain main shop menu
         dw C3B4BD       ; 26: Handle buy item list
         dw C3B505       ; 27: Handle buy order menu
         dw C3B60E       ; 28: Return to Buy menu after delay
         dw C3B616       ; 29: Handle sell item list
         dw C3B676       ; 2A: Handle sell order menu
         dw C3B752       ; 2B: Return to Sell menu after delay
         dw C37A87       ; 2C: Erase warning in Lineup menu after delay
         dw C37168       ; 2D: Handle Lineup menu choice 1
         dw C3720C       ; 2E: Handle Lineup menu choice 2
         dw C38A84       ; 2F: Initialize item usage menu
         dw C38A96       ; 30: Sustain item usage menu
         dw C3ACAA       ; 31: Initialize Colosseum item menu
         dw C328D3       ; 32: Handle esper selection
         dw C358CE       ; 33: Sustain esper data menu
         dw C3293A       ; 34: Wait while showing who holds esper
         dw C31BB8       ; 35: Initialize Equip menu
         dw C39621       ; 36: Handle Equip menu options
         dw C31EB5       ; 37: Handle multitargeting in main menu
         dw C31C32       ; 38: Initialize party gear overview
         dw C32966       ; 39: Sustain party gear overview
         dw C32A65       ; 3A: Initialize spell usage menu
         dw C32AAE       ; 3B: Handle single-target spell
         dw C32D1C       ; 3C: Return to Magic menu
         dw C32D78       ; 3D: Handle multitarget spell
         dw C3298E       ; 3E: Sustain SwdTech menu
         dw C39FB1       ; 3F: Handle delayed fade-out for Reequip
         dw C31C1A       ; 40: Open Equip menu after optimizing gear
         dw C31C26       ; 41: Open Equip menu after removing all gear
         dw C3631D       ; 42: Initialize Lineup's Status menu
         dw C3633F       ; 43: Sustain Lineup's Status menu
         dw C37131       ; 44: Return to Lineup menu from Status menu
         dw C3A9F8       ; 45: Initialize final battle lineup
         dw C3AA25       ; 46: Sustain final battle lineup
         dw C342C2       ; 47: Initialize Cmd.Set menu
         dw C342DF       ; 48: Handle Cmd.Set menu member selection
         dw C34338       ; 49: Handle Cmd.Set menu command choice 1
         dw C34376       ; 4A: Handle Cmd.Set menu command choice 2
         dw C346AD       ; 4B: Initialize Controller menu
         dw C346CA       ; 4C: Sustain Controller menu
         dw C32977       ; 4D: Sustain Blitz menu
         dw C3ADB7       ; 4E: Initialize Colosseum matchup menu
         dw C3AE0F       ; 4F: Sustain Colosseum matchup menu
         dw C3240C       ; 50: Handle Order menu choice 1
         dw C32453       ; 51: Handle Order menu choice 2
         dw C31A0A       ; 52: Queue fade-in for save menu
         dw C319FB       ; 53: Queue fade-out for save menu
         dw C31A19       ; 54: Wait while save menu fades in or out
         dw C398CF       ; 55: Handle manual gear removal
         dw C39884       ; 56: Handle selection of gear slot to fill
         dw C3990F       ; 57: Handle gear browsing
         dw C39E4B       ; 58: Initialize Relic menu
         dw C39EB8       ; 59: Handle Relic menu options
         dw C39FFD       ; 5A: Handle selection of relic slot to fill
         dw C3A097       ; 5B: Handle relic browsing
         dw C3A10A       ; 5C: Handle relic removal
         dw C3652D       ; 5D: Initialize actor naming menu
         dw C389E6       ; 5E: Sustain shifted gear data menu
         dw C3656C       ; 5F: Sustain actor naming menu
         dw C31BE5       ; 60: Swap actor in Equip menu, retain Equip mode
         dw C31BF3       ; 61: Swap actor in Equip menu, retain Remove mode
         dw C39E7D       ; 62: Swap actor in Relic menu, retain Equip mode
         dw C39E8B       ; 63: Swap actor in Relic menu, retain Remove mode
         dw C31AD6       ; 64: Initialize Item menu
         dw C336E7       ; 65: Shift screen
         dw C379FF       ; 66: Queue fade-in for Lineup menu
         dw C379F0       ; 67: Queue fade-out for Lineup menu
         dw C37A0E       ; 68: Wait while Lineup menu fades in or out
         dw C370E7       ; 69: Initialize Lineup menu

; Set to exit menu if event countdown timer expired
C302DB:  LDA $B4         ; Already handled?
         BNE C369DB      ; Exit if so
         LDA $1188       ; Timer 0 flags
         BIT #$20        ; Abort if expired?
         BEQ C369DB      ; Exit if not
         LDY $1189       ; Timer expired?
         BNE C369DB      ; Exit if not
         LDA #$05        ; Origin: Timer
         STA $0205       ; Set exit reason
         INC $B4         ; Disable this fn
         JMP C3B4B6      ; Next: Fade-out

; Draw 3 text strings
drawx3:  LDA #$03        ; Strings: 3

; Draw multiple strings using consecutive pointers
C369BA:  PHA             ; Save counter
         PHX             ; Save index
         REP #$20        ; 16-bit A
         LDA.L C30000,X  ; Text pointer
         TAY             ; Put in Y
         JSR C302F9      ; Draw string
         PLX             ; String index
         INX             ; Index +1
         INX             ; Index +1
         PLA             ; Strings left
         DEC A           ; One less left
         BNE C369BA      ; Loop till last
C369DB:  RTS

; Draw text string in user's color
draw20:  LDA #$20        ; Palette 0
         BRA C302F_      ; Draw string

; Draw text string in gray or blue
draw24:  LDA #$24        ; Palette 1
         BRA C302F_      ; Draw string

; Draw text string in gray
draw28:  LDA #$28        ; Palette 2
         BRA C302F_      ; Draw string

; Draw text string in blue (V1)
draw2c:  SEP #$20        ; 8-bit A
         LDA #$2C        ; Palette 3
         BRA C302F_      ; Draw string

; Draw text string in blue (V2)
draw30:  LDA #$30        ; Palette 4
C302F_:  STA $29         ; Set attribs

; Load text string into tilemap
C302F9:  SEP #$20        ; 8-bit A
         LDA #$C3        ; Src bank: C3
C302FB:  JSR E7_35C_     ; Define source
;        LDY $00         ; Src index: 0
         REP #$20        ; 16-bit A
         LDA [$E7]       ; Tilemap ptr
         STA $2181       ; Set WRAM LBs
         SEP #$20        ; 8-bit A
         INY             ; Src index +1
C30312:  INY             ; Src index +1
         LDA [$E7],Y     ; Text character
         BEQ C369DB      ; Exit if <end>
         STA $2180       ; Add to tilemap
         LDA $29         ; Palette, etc.
         STA $2180       ; Add to tilemap
         BRA C30312      ; Load next char

; Draw full-screen window
mainbox: LDX #C331C7     ; C3/31C7

; Draw a menu window
C30341:  LDA #$C3        ; Bank: C3
C30343:  STA $E9         ; Set src HB
C30345:  STX $E7         ; Set src LBs
         LDY #$0002      ; Index: 2
         LDA [$E7],Y     ; Inner width
         STA $EF         ; Memorize it
         STZ $F0         ; Clear HB
         INY             ; Index +1
         LDA [$E7],Y     ; Inner height
         STA $F1         ; Memorize it
         STZ $F2         ; Clear HB
         JSR db7e_       ; Set DB to 7E
         LDA #$7E        ; Bank: 7E
         STA $ED         ; Set ptr HB
         REP #$20        ; 16-bit A
         LDA [$E7]       ; Tilemap ptr
         STA $EB         ; Set position

; Fork: Draw upper row
C303A3:  LDY $00         ; Map index: 0
         LDA $9F3F       ; NE corner tile
         STA $E5         ; Set final tile
         LDA $9F39       ; NW corner tile
         TYX             ; Clear X
         INX             ; Tiles: 2
         STX $F5         ; Set pattern width
         LDX #$0022      ; Base tile index
         JSR C30428      ; Draw top row

; Fork: Draw inner rows
C30375:  TDC             ; Inner row: 1st
         LDY #$0040      ; Skipped rows: 1
         STY $F3         ; Init map index
         INC $F5         ; Pattern width +1
         INC $F5         ; Pattern width +1
C3037F:  PHA             ; Save row number
         AND #$0003      ; Get pattern row
         ASL A           ; x2
         PHA             ; Memorize it
         ASL A           ; x4
         ASL A           ; x8
         TAX             ; Set tile index
         PLA             ; Pattern row x2
         AND #$0002      ; A=2 if even row
         ADC $9F43       ; Add base tile
         STA $E5         ; Set final tile
         DEC A           ; Left border B
         JSR C30428      ; Draw inner row
;        SEC             ; Offset ADC
         LDA $F3         ; Tilemap index
         ADC #$003F      ; Go 1 row down
         STA $F3         ; Save changes
         TAY             ; Index it
         PLA             ; Row number
         INC A           ; Row number +1
         CMP $F1         ; At last row?
         BNE C3037F      ; Loop if not

; Fork: Draw lower row
C303BB:  DEC $F5         ; Pattern width -1
         DEC $F5         ; Pattern width -1
         LDA $9F4F       ; SE corner tile
         STA $E5         ; Set final tile
         LDA $9F49       ; SW corner tile
         LDX #$0032      ; Base tile index
         JSR C30428      ; Draw bottom row
         JMP C3134C      ; 8-bit A; pull DB

; Draw a row of window tiles
C30428:  STA [$EB],Y     ; Set left tile
         STX $E0         ; Memorize index
         TDC             ; Inner col: 1st
C30430:  INY             ; Map index +1
         INY             ; Map index +1
         CMP $EF         ; At last column?
         BEQ C30449      ; Branch if so
         PHA             ; Save column
         AND $F5         ; Stay in pattern
         ASL A           ; Double it
         ADC $E0         ; Add base index
         TAX             ; Index sum
         LDA $9F19,X     ; Inner tile
         STA [$EB],Y     ; Put in tilemap
         PLA             ; Column number
         INC A           ; Column +1
         BRA C30430      ; Do next tile
C30449:  LDA $E5         ; Rightmost tile
         STA [$EB],Y     ; Add to tilemap
         RTS

; Draw 3 digits (16-bit number)
C30486:  PEA $0002       ; Skipped: 2
         BRA C3049C      ; Branch below

; Draw 4 digits
C30490:  PEA $0001       ; Skipped: 1
         BRA C3049C      ; Branch below

; Draw 5 digits
C3049A:  PEI ($00)       ; Skipped: 0
C3049C:  LDY #$0005      ; Digits: 5
         BRA C304C5      ; Draw text

; Draw 8 digits
C304A3:  PEI ($00)       ; Skipped: 0
         BRA C304AF      ; Branch below

; Draw 7 digits
C304AC:  PEA $0001       ; Skipped: 1
C304AF:  LDY #$0008      ; Digits: 8
         BRA C304C5      ; Draw text

; Draw 2 digits
C304B_:  PHX             ; Save position
         JSR C304E0      ; Isolate digits
         PLX             ; Text position
C304B6:  PEA $0001       ; Skipped: 1
         BRA C304C2      ; Branch below

; Draw 3 digits (8-bit number)
C304CX:  PHX             ; Save position
C304CY:  JSR C304E0      ; Isolate digits
         PLX             ; Text position
C304C0:  PEI ($00)       ; Skipped: 0
C304C2:  LDY #$0003      ; Digits: 3
C304C5:  STY $E0         ; Set counter
         STX $EB         ; Set dst LBs
         SEP #$20        ; 8-bit A
         LDA #$7E        ; Bank: 7E
         STA $ED         ; Set dst HB
         PLX             ; Digit index
         LDY $00         ; Map index: 0
C304D0:  LDA $F7,X       ; Digit char
         STA [$EB],Y     ; Add to tilemap
         INY             ; Map index +1
         LDA $29         ; Palette, etc.
         STA [$EB],Y     ; Set attributes
         INY             ; Map index +1
         INX             ; Digit index +1
         CPX $E0         ; More digits?
         BNE C304D0      ; Loop if so
         RTS

; Convert minutes played into text
C304F_:  LDA $1864       ; Minutes played

; Convert 8-bit number into text, retain leading zeroes
C304F9:  STA $F3         ; Save number
         STZ $F4         ; Clear HB
         LDA #$03        ; Digits: 3
         LDX #$0004      ; ROM index: 4
C30533:  LDY $00         ; RAM index: 0
C30535:  PHA             ; Save counter
         REP #$20        ; 16-bit A
         LDA.L C305E4,X  ; Power of 10
         STA $ED         ; Memorize it
         INX             ; ROM index +1
         INX             ; ROM index +1
         STZ $EB         ; Digit: 0
C30541:  SEC             ; Prepare SBC
         LDA $F3         ; Revised number
         SBC $ED         ; Deduct power
         BCC C3054E      ; Branch if < 0
         STA $F3         ; Save changes
         INC $EB         ; Digit +1
         BRA C30541      ; Loop

; Fork: Convert digit into text
C3054E:  SEP #$20        ; 8-bit A
         LDA $EB         ; Digit
         ADC.B #!num0    ; Turn into text
         STA $00F7,Y     ; Set digit char
         INY             ; RAM index +1
         PLA             ; Digits left
         DEC A           ; One less left
         BNE C30535      ; Loop till last
         RTS

; Define MP cost, turn into text
mptext:  JSR C3510D      ; Define MP cost

; Convert 8-bit number into text, blank leading zeroes
C304E0:  JSR C304F9      ; Turn into text
         LDY #$0002      ; Digits left: 2
         BRA C305CC      ; Blank zeroes

; Convert 16-bit number into text, blank leading zeroes
C3052E:  SEP #$20        ; 8-bit A
         LDA #$05        ; Digits: 5
         LDX $00         ; ROM index: 0
         JSR C30533      ; Turn into text

; Fork: Blank leading zeroes
C30562:  LDY #$0004      ; Digits left: 4
         BRA C305CC      ; Blank zeroes

; Convert HP or MP into text
hpmptxt: LDX $09,Y       ; HP or MP
         STX $F3         ; Memorize it
         BRA C3052E      ; Turn into text

; Convert owned GP into text
C3058X:  JSR C38085      ; Color: User's
         LDY $1860       ; Gold LBs
         LDA $1862       ; Gold HB

; Convert 24-bit number into text, blank leading zeroes
C3058_:  STY $F1         ; Number B1, B2
         STA $F3         ; Number B3
C30582:  STZ $F4         ; Number B4: 0
         LDY $00         ; RAM index: 0
         TYX             ; ROM index: 0
         LDA #$08        ; Digits: 8
C3058B:  PHA             ; Save counter
         REP #$20        ; 16-bit A
         STZ $EB         ; Digit: 0
C3058F:  SEC             ; Prepare SBC
         LDA $F1         ; Number LBs
         STA $E1         ; Memorize them
         SBC.L C305DE,X  ; Deduct power's
         STA $F1         ; Save changes
         LDA $F3         ; Number HBs
         SBC.L C305EE,X  ; Deduct power's
         BCC C305A6      ; Branch if < 0
         STA $F3         ; Save changes
         INC $EB         ; Digit +1
         BRA C3058F      ; Loop

; Fork: Cancel subtraction and convert digit
C305A6:  LDA $E1         ; Number LBs
         STA $F1         ; Restore them
         SEP #$21        ; 8-bit A; C+
         LDA $EB         ; Digit
         ADC.B #!num0-1  ; Turn into text
         STA $00F7,Y     ; Set digit char
         INY             ; RAM index +1
         INX             ; ROM index +1
         INX             ; ROM index +1
         PLA             ; Digits left
         DEC A           ; One less left
         BNE C3058B      ; Loop till last

; Fork: Blank leading zeroes
C305C8:  LDY #$0007      ; Digits: 7
C305CC:  LDX $00         ; Index: 0
C305CD:  LDA $F7,X       ; Digit char
         CMP.B #!num0    ; Zero?
         BNE C305DD      ; Exit if not
         LDA #$FF        ; Space char
         STA $F7,X       ; Replace digit
         INX             ; Point to next
         DEY             ; One less left
         BNE C305CD      ; Loop till last
C305DD:  RTS

; Powers of ten, low bytes
C305DE:  dw $9680        ; 10000000
         dw $4240        ; 1000000
         dw $86A0        ; 100000
C305E4:  dw $2710        ; 10000
         dw $03E8        ; 1000
C305E8:  dw $0064        ; 100
         dw $000A        ; 10
         dw $0001        ; 1

; Powers of ten, high bytes
C305EE:  dw $0098        ; 10000000
         dw $000F        ; 1000000
         dw $0001        ; 100000
         dw $0000        ; 10000
         dw $0000        ; 1000
         dw $0000        ; 100
         dw $0000        ; 10
         dw $0000        ; 1

; Set address to table of cursor coordinates
C30630:  SEP #$20        ; 8-bit A
         LDA #$C3        ; Bank: C3
         JMP C3D51D      ; Src: C3/YYYY

; Position cursor in party area of Lineup menu
C30637:  TDC             ; Clear A
         LDA $54         ; Number of rows
         TAX             ; Set counter
         LDA $4E         ; Cursor row
.loop    CLC             ; Prepare ADC
         ADC $4D         ; Add cursor column
         DEX             ; One less row
         BNE .loop       ; Loop till last
         BRA C3064B      ; Set src, coords

; Position cursor in unscrollable area
C30640:  JSR C30630      ; Set source
C30643:  JSR curloc      ; Compute position
         STA $4B         ; Set cursor slot
         BRA C306C5      ; Set coords

; Position cursor in scrollable list
C30648:  TDC             ; Clear A
         LDA $53         ; Number of columns
         TAX             ; Set counter
         LDA $4F         ; List column
.loop    CLC             ; Prepare ADC
         ADC $50         ; Add list row
         DEX             ; One less column
         BNE .loop       ; Loop till last
C3064B:  STA $4B         ; Set cursor slot
         JSR C30630      ; Set source

; Fork: Set cursor's position in pixels
C306C5:  JSR curloc      ; Compute position
         ASL A           ; Double it
         TAY             ; Index it
         REP #$20        ; 16-bit A
         LDA [$E7],Y     ; Coordinates
         STA $55         ; Set cursor's
         SEP #$20        ; 8-bit A
         RTS

; Compute cursor's slot position in unscrollable area
curloc:  TDC             ; Clear A
         LDA $53         ; Number of columns
         TAY             ; Set counter
         LDA $4D         ; Cursor column
.loop    CLC             ; Prepare ADC
         ADC $4E         ; Add cursor row
         DEY             ; One less column
         BNE .loop       ; Loop till last
padout:  RTS

; Handle D-Pad for unscrollable list
C3072D:  JSR key_up      ; Pushing up?
         BEQ C30750      ; Branch if not
         LDA $4E         ; On top row?
         BNE C30748      ; Go up if not
         LDA $59         ; Wrap flags
         LSR A           ; No V-Wrap?
         BCS padout      ; Exit if so
         LDA $54         ; Bottom row +1
         STA $4E         ; Set cursor row

; Fork: Move cursor up
C30748:  DEC $4E         ; Cursor row -1
         BRA C307AC      ; Sound: Cursor

; Fork: Handle down
C30750:  BIT #$04        ; Pushing down?
         BEQ C30773      ; Branch if not
         LDA $54         ; Bottom row
         DEC A           ; Adjust value
         CMP $4E         ; Cursor's there?
         BNE C3076B      ; Go down if not
         LDA $59         ; Wrap flags
         LSR A           ; No V-Wrap?
         BCS padout      ; Exit if so
         STZ $4E         ; Cursor row: 1
         BRA C307AC      ; Sound: Cursor

; Fork: Move cursor down
C3076B:  INC $4E         ; Cursor row +1
         BRA C307AC      ; Sound: Cursor

; Fork: Handle left
C30773:  BIT #$02        ; Pushing left?
         BEQ C30792      ; Branch if not
         LDA $4D         ; On first column?
         BNE C3078B      ; Go left if not
         LDA $59         ; H-Wrap disabled?
         BMI padout      ; Exit if so
         LDA $53         ; Last column +1
         STA $4D         ; Set cursor column

; Fork: Move cursor left
C3078B:  DEC $4D         ; Cursor column -1
         BRA C307AC      ; Sound: Cursor

; Fork: Handle right
C30792:  LSR A           ; Pushing right?
         BCC padout      ; Exit if not
         LDA $53         ; Last column
         DEC A           ; Adjust value
         CMP $4D         ; Cursor's there?
         BNE C307AA      ; Go right if not
         LDA $59         ; H-Wrap disabled?
         BMI padout      ; Exit if so
         STZ $4D         ; Cursor column: 1
         BRA C307AC      ; Sound: Cursor

; Fork: Move cursor right
C307AA:  INC $4D         ; Cursor column +1
C307AC:  JMP C30EA3      ; Sound: Cursor

; Update movable cursor's sprite
C307B8:  BNE C307DC      ; If mode 1

; Mode 0: Initialize cursor
C307C0:  LDY #C3088D     ; Anim table ptr
         JSR initob0     ; Initialize data

; Mode 1: Sustain cursor
C307DC:  CLC             ; Set to dequeue
         LDA $46         ; Menu flags
         AND #$06        ; Remove cursors?
         BEQ C30864      ; Delete if so
         LDA $45         ; Menu flags
         BIT #$04        ; Hide cursor?
         BEQ C30863      ; Exit if so
         LDA $55         ; Cursor's X
         STA $33CA,X     ; Set sprite's
         LDA $56         ; Cursor's Y
         STA $344A,X     ; Set sprite's
         BRA C30860      ; Set OAM; requeue

; Update blinking cursor marking prior choice
C307FF:  BNE C3082B      ; If mode 1

; Mode 0: Initialize cursor
C3080B:  LDA #$01        ; Blinkers V1: On
         TSB $46         ; Set menu flag
         LDY #C30896     ; Anim table ptr
         JSR initob0     ; Initialize data

; Mode 1: Sustain cursor
C3082B:  LDA $46         ; Menu flags
         LSR A           ; Remove blinkers?
         BCC C30864      ; Delete if so
         LDA $344A,X     ; Cursor's Y
         SBC $41         ; Deduct list speed
         STA $344A,X     ; Set cursor's Y
         LDA $46         ; Menu flags
         AND #$C0        ; Scrollable list?
         BEQ C30860      ; Display if not

; Fork: Item menu
C3084A:  LDA $28         ; Blinker's slot
         SEC             ; Prepare SBC
         SBC $4A         ; Above list page?
         BCC C30863      ; Blank if so
         SBC #$0A        ; Below list page?
         BCS C30863      ; Blank if so
C30860:  JSR C31221      ; Define OAM
C30863:  SEC             ; Set to requeue
C30864:  RTS

; Update blinking cursor not marking a prior choice
C308D1:  BNE C308F9      ; If mode 1

; Mode 0: Initialize cursor
C308D9:  LDA #$08        ; Blinkers V2: On
         TSB $46         ; Set menu flag
         LDY #C30896     ; Anim table ptr
         JSR initob0     ; Initialize data

; Mode 1: Sustain cursor
C308F9:  CLC             ; Set to dequeue
         LDA $46         ; Menu flags
         BIT #$08        ; Remove blinkers?
         BEQ C30864      ; Delete if so
         BRA C30860      ; Define OAM

; Create a scrollbar on right side of screen
C3091F:  JSR C30933      ; Queue OAM fn
         LDA #$E8        ; X: 232
         BRA C30940      ; Set sprite's

; Create a scrollbar in middle of screen
C30933:  LDY #C30947     ; C3/0947
         JSR qanim3      ; Queue OAM fn
C3093D:  LDA #$78        ; X: 120
C30940:  STA $7E33CA,X   ; Set sprite's
C30946:  RTS

; Update scrollbar
C30947:  BNE C3096A      ; If mode 1

; Mode 0: Initialize scrollbar
C3094F:  LDA #$C0        ; Flash: Both ways
         TSB $46         ; Init anim index
         LDY #C309E3     ; Anim table ptr
         JSR initob1     ; Set pose timer

; Mode 1: Sustain scrollbar
C3096A:  LDA $46         ; Menu flags
         CMP #$40        ; Hide scrollbar?
         BCC C30946      ; Delete if so

; Fork: Update scrollbar animation index
C30908:  LDA #$C0        ; Flashing: 2-ways
         TSB $46         ; Initialize index
         LDA $4A         ; At top of list?
         BNE C30914      ; Branch if not
         LDA #$40        ; Flashing: Down
         BRA C3091C      ; Update index
C30914:  CMP $5C         ; At very bottom?
         BNE C3097D      ; Branch if not
         LDA #$80        ; Flashing: Up
C3091C:  TRB $46         ; Update index

; Fork: Update object data
C3097D:  LDY $354A,X     ; V-Speed
         LDA $4A         ; Scroll position
         JSR mul16       ; Speed x position
         TDC             ; Addend: 0
         LDX $49         ; Scroll position
         BPL C309AC      ; Branch if < 128
         LDA #$70        ; Addend: 112
C309AC:  CLC             ; Prepare ADC
         ADC $002135     ; Add product MB
         LDX $2D         ; Queue index
         CLC             ; Prepare ADC
         ADC $34CA,X     ; Add base Y
         STA $344A,X     ; Set bar's Y
;        TDC             ; Clear A
         LDA $46         ; Menu flags
         AND #$C0        ; Anim index
         JSR div32       ; Put in b1+b2
         TAX             ; Index it
         REP #$20        ; 16-bit A
         LDA.L C309DB-2,X; Anim pointer
         JMP C339BD      ; Set OAM; requeue

; Create portrait for member slot
C30A22:  PHX             ; Save face slot
         JSR C361D_      ; Queue OAM fn
         PLY             ; Face slot
         STA $0060,Y     ; Save queue index
         PHB             ; Save DB
         JSR C30AF1      ; Set base X pos
         JSR C30ACC      ; Load anim ptr
         PHX             ; Save index
         TYX             ; Face slot
         LDA.L face_y,X  ; Base Y pos
         PLX             ; Queue index
         BRA C361D0      ; Set Y, pose timer

; Create portrait by upper-left corner
C361AC:  JSR C361DA      ; Queue OAM fn
         PHB             ; Save DB
         LDA $28         ; Member slot
         TAY             ; Put it in Y
         JSR C30AF1      ; Set X position
C361FB:  JSR C30ACC      ; Load anim ptr
         LDA #$38        ; Y: 56
         BRA C361D0      ; Set pose timer

; Create portrait for actor naming menu
C366D8:  JSR C361D_      ; Queue OAM fn
         JSR db7e_       ; Set DB to 7E
         TAY             ; Face slot: 1
         JSR C30ACC      ; Set anim ptr
         LDA #$10        ; Coord: 16
         STA $33CA,X     ; Set X position
         BRA C361D0      ; Set Y, pose timer

; Create portrait for Equip or Relic menu
C361B2:  JSR C361DA      ; Queue OAM fn
         JSR db7e_       ; Set DB to 7E
         LDA #$C8        ; X: 200
         STA $33CA,X     ; Set sprite's
         LDA $28         ; Member slot
         TAY             ; Put it in Y
         JSR C30ACC      ; Set anim ptr
         LDA #$30        ; Y: 48
C361D0:  STA $344A,X     ; Set sprite's
         JSR C31206      ; Set pose timer
         PLB             ; Restore DB
         RTS

; Load pointer to animation table for portrait
C30ACC:  PHX             ; Save index
         TDC             ; Clear A
         LDA $0075,Y     ; Party info
         AND #$18        ; Roster slot
         LSR A           ; Put in b2-b3
         LSR A           ; Put in b2-b3
         TAX             ; Index it
         REP #$20        ; 16-bit A
         LDA.L C30AE9,X  ; Anim table ptr
         PLX             ; Queue index
         PHY             ; Save Y
         TAY             ; Put ptr in Y
         JSR setani_     ; Set anim ptr
         PLY             ; Restore Y
         RTS

; Set portrait's base X position based on actor's row
C30AF1:  JSR db7e        ; Set DB to 7E
         LDA #$02        ; Portrait bit
         BIT $45         ; Ignore row?
         BNE C30AFF      ; Branch if so
         LDA $0075,Y     ; Party info
C30AFB:  BIT #$20        ; In back row?
         BEQ C30B06      ; Branch if not
C30AFF:  LDA #$1A        ; Base X: 26
         BRA C30B0B      ; Skip a line
C30B06:  LDA #$0E        ; Base X: 15
C30B0B:  STA $33CA,X     ; Set sprite's X
         RTS

; Update actor portrait
C30B12:  LSR A           ; Mode 0?
         BEQ C30B20      ; Branch if so
         DEC A           ; Mode 1?
         BEQ C30B2A      ; Branch if so
         DEC A           ; Mode 2?
         BEQ C30B4C      ; Branch if so
         DEC A           ; Mode 4?
         BNE C30B5E      ; Branch if so

; Mode 3: Set to shift right
C30B38:  LDA #$01        ; H-Speed: 1
         BRA C30B53      ; Shift once

; Mode 2: Set to shift left
C30B4C:  LDA #$FF        ; H-Speed: -1
C30B53:  STA $34CA,X     ; Set sprite's
         LDA #$0C        ; Timer: 12
         STA $3349,X     ; Set sprite's

; Mode 4: Shift left or right
C30B5E:  LDA #$04        ; Mode: 4
         STA $3649,X     ; Set sprite's
         LDA $3349,X     ; Timer expired?
         BEQ C30B80      ; Freeze if so
         LDA $34CA,X     ; H-Speed
         CLC             ; Prepare ADC
         ADC $33CA,X     ; Add base X
         STA $33CA,X     ; Set sprite's
         DEC $3349,X     ; Timer -1
         BRA C30B87      ; Define OAM

; Fork: Set to stop moving
C30B80:  JSR C382D6      ; Next mode: 1
C30B87:  JMP C30860      ; Set OAM; requeue

; Mode 0: Set to pan with BG1
C30B20:  INC $3649,X     ; Mode +1
         JSR C3EA6C      ; Follow BG1: Y

; Mode 1: Sustain portrait
C30B2A:  LDA $35C9,X     ; Hide portrait?
         BPL C30B87      ; Display if not
         CLC             ; Set to dequeue
         RTS

; Draw member's level, HP, and MP
C30C6C:  STX $EF         ; Pos tbl ptr LBs
         LDA #$C3        ; Bank: C3
         STA $F1         ; Set ptr HB
         LDX $67         ; Actor's address
         LDA $08,X       ; Level
         JSR C304E0      ; Turn into text
         REP #$20        ; 16-bit A
         LDA [$EF]       ; Tilemap ptr
         TAX             ; Move it to X
         JSR C304B6      ; Draw level
         JSR boosth_     ; Define max HP
         JSR C3052E      ; Turn into text
         LDY #$0004      ; Map ptr index
         JSR C30D21      ; Draw max HP
         LDY $67         ; Actor's address
         JSR C32B9A      ; Truncate HP
         JSR hpmptxt     ; Turn into text
         LDY #$0002      ; Map ptr index
         JSR C30D21      ; Draw HP
         JSR C30D2B      ; Can use magic?
         BCC C30CEF      ; Hide MP if not
         JSR boostm_     ; Define max MP
         JSR C3052E      ; Turn into text
         LDY #$0008      ; Map ptr index
         JSR C30D21      ; Draw max MP
         LDY $67         ; Actor's address
         JSR C32BBC      ; Truncate MP
         INY             ; Stat index +1
         INY             ; Stat index +1
         INY             ; Stat index +1
         INY             ; Stat index +1
         JSR hpmptxt     ; Turn into text
         LDY #$0006      ; Map ptr index
C30D21:  REP #$20        ; 16-bit A
         LDA [$EF],Y     ; Tilemap ptr
         TAX             ; Move it to X
         JMP C30490      ; Draw 4 digits

; Fork: Hide MP
C30CEF:  REP #$21        ; 16-bit A; C-
         LDY #$0006      ; Point to MP ptr
         LDA [$EF],Y     ; Tilemap ptr
         SBC #$0005      ; Point to "MP"
         JSR C3BAB3      ; Set position
         LDX #$000C      ; Spaces: 12
         JMP C357F3      ; Draw 12 spaces

; Determine whether actor can use magic
C30D2B:  LDX $1A69       ; Espers LBs
         BNE C30D63      ; OK if got any
         LDX $1A6B       ; Espers HBs
         BNE C30D63      ; OK if got any
         LDY $67         ; Actor's address
         JSR gogoch2     ; Gogo?
         BEQ C30D63      ; Allow if so
         BCS C30D61      ; Deny Umaro/guest
         XBA             ; Put actor in B
         LDA #$36        ; Spell list length
         JSR C350F_      ; X = Actor x length
         LDY #$0036      ; Spells left: 54
C30D56:  LDA $1A6E,X     ; Spell mastery
         BMI C30D63      ; Allow if 100%
         INX             ; Point to next
         DEY             ; One less left
         BNE C30D56      ; Loop till last
C30D61:  CLC             ; Access: No
         RTS             ; Exit
C30D63:  SEC             ; Access: Yes
         RTS

; Determine max HP with gear bonus
boosth_: LDY $67         ; Actor's address
boosthp: REP #$20        ; 16-bit A
         LDA $000B,Y     ; Max HP
         JSR C30D65      ; Apply bonus
C30D92:  CMP #$2710      ; Under 10000?
         BCC C30DA9      ; Exit if so
         LDA #$270F      ; Enforce 9999
         BRA C30DA9      ; Save max HP

; Determine max MP with gear bonus
boostm_: LDY $67         ; Actor's address
boostmp: REP #$20        ; 16-bit A
         LDA $000F,Y     ; Max MP
         JSR C30D65      ; Apply bonus
C30D9F:  CMP #$03E8      ; Under 1000?
         BCC C30DA9      ; Exit if so
         LDA #$03E7      ; Enforce 999
C30DA9:  STA $F3         ; Save max MP
         BRA SEP0DB      ; 8-bit A; exit

; Determine max HP or MP with gear bonus
C30D65:  ASL A           ; Shift bits
         ROL A           ; Shift bits
         PHA             ; Save result
         ROL A           ; Flags to b0-b1
         ROL A           ; Flags to b1-b2
         AND #$0006      ; Bonus flags
         TAX             ; Index them
         PLA             ; Saved value
         LSR A           ; x4 -> x2
         LSR A           ; x1 without flags
         STA $E7         ; Memorize HP or MP
         JMP (C30D7E,X)  ; Apply bonus

; Jump table for the above
C30D7E:  dw C30D86       ; No bonus
         dw C30D88       ; HP or MP + 25%
         dw C30D89       ; HP or MP + 50%
         dw C30D87       ; HP or MP + 12.5%

; Boost max HP or MP based on entry point
C30D86:  TDC             ; Zero bonus
C30D87:  LSR A           ; Halve bonus
C30D88:  LSR A           ; Halve bonus
C30D89:  LSR A           ; Halve bonus
         CLC             ; Prepare ADC
         ADC $E7         ; Add max value
         RTS

; Interchange cursor memory for swapped members
C30DBA:  JSR C3BFC2      ; X = Slot B
         LDA $28         ; Chosen slot A
         TAY             ; Index it
         LDA $0246,X     ; B's Magic bar
         PHA             ; Memorize pos
         LDA $0246,Y     ; A's Magic bar
         STA $0246,X     ; Set as B's
         PLA             ; B's Magic bar
         STA $0246,Y     ; Set as A's
         REP #$20        ; 16-bit A
         TXA             ; Chosen slot B
         ASL A           ; Double it
         TAX             ; Reindex it
         TYA             ; Chosen slot A
         ASL A           ; Double it
         TAY             ; Reindex it
         LDA $0236,X     ; B's Skills pos
         PHA             ; Memorize it
         LDA $0236,Y     ; A's Skills pos
         STA $0236,X     ; Set as B's
         PLA             ; B's Skills pos
         STA $0236,Y     ; Set as A's
         LDA $023E,X     ; B's Magic pos
         PHA             ; Memorize it
         LDA $023E,Y     ; A's Magic pos
         STA $023E,X     ; Set as B's
         PLA             ; B's Magic pos
         STA $023E,Y     ; Set as A's
SEP0DB:  SEP #$20        ; 8-bit A
         RTS

; Restore cursor position in inventory
C30DAC:  LDY $022F       ; Old list pos
         LDA $0231       ; Old scroll pos
         BRA C30E1E      ; Restore them

; Restore cursor position in Magic menu
C30E0A:  JSR ld28x2      ; X = Mbr slot x2
         LDY $023E,X     ; Old list pos
         LSR A           ; Member slot
         TAX             ; Index it
         LDA $0246,X     ; Old scroll pos
C30E1E:  STA $4A         ; Set scroll pos
         STY $4F         ; Set list pos
         STY $4D         ; Set onscreen col
         LDA $50         ; List row
         SEC             ; Prepare SBC
         SBC $4A         ; Deduct scroll pos
         STA $4E         ; Set onscreen row
C30E27:  RTS

; Play cursor sound
C30EA3:  LDA $AE         ; Started already?
         BNE C30E27      ; Exit if so
C30EA9:  LDA #$21        ; APU command
         STA $AE         ; Prevent repeat
         BRA IOP0        ; Set I/O port 0

; Play click sound
C30EB2:  LDA #$20        ; APU command
         BRA IOP0        ; Set I/O port 0

; Play save sound
C30EB9:  LDA #$23        ; APU command
         BRA IOP0        ; Set I/O port 0

; Play buzzer sound
C30EC0:  LDA #$22        ; APU command
         BRA IOP0        ; Set I/O port 0

; Play letter deletion sound
C30EC7:  LDA #$24        ; APU command
IOP0:    STA $002140     ; Set I/O port 0
         RTS

; Play cash register sound
C30ECE:  LDX #$BF18      ; Cmd + Sound ID
C30ED6:  STX $1300       ; Set ports 0 & 1
         LDA #$80        ; Volume specs
         STA $1302       ; Set I/O port 2
C30EE0:  JSL $C50004     ; Update ports
         TDC             ; Clear A
         RTS

; Play recovery sound
C30EE5:  LDX #$C518      ; Cmd + Sound ID
         BRA C30ED6      ; Set vol, etc.

; Adjust volume based on sound mode (stereo = 0)
C33E3F:  STA $1301       ; Set sound mode
         LDA #$F3        ; SPC code: $0D61
         STA $1300       ; Set SPC command
         BRA C30EE0      ; Update ports

; Set to upload BG1 tilemaps A and B
C30EE9:  PEA $1000       ; Bytes: 4096
         BRA C30EF_      ; Set DMA vars

; Set to upload BG1 tilemap A
C30EFD:  PEA $0800       ; Bytes: 2048
C30EF_:  LDY $00         ; VRAM: $0000
         LDX #$3849      ; Src: 7E/3849
         BRA C30F4Y      ; Set DMA vars

; Set to upload BG1 tilemap B
C30F11:  LDY #$0400      ; VRAM: $0400
         LDX #$4049      ; Src: 7E/4049
         BRA C30F4X      ; Set DMA vars

; Set to upload BG3 tilemaps A and B
C30F25:  PEA $1000       ; Bytes: 4096
         BRA C30F3_      ; Set DMA vars

; Set to upload BG3 tilemap A (version 1)
C30F39:  PEA $0800       ; Bytes: 2048
C30F3_:  LDY #$4000      ; VRAM: $4000
         LDX #$7849      ; Src: 7E/7849
         BRA C30F4Y      ; Set DMA vars

; Set to upload BG3 tilemap B (version 1)
C30F4D:  LDY #$4400      ; VRAM: $4400
         LDX #$8049      ; Src: 7E/8049
C30F4X:  PEA $0800       ; Bytes: 2048
C30F4Y:  STY $14         ; Set VRAM ptr
         STX $16         ; Set src LBs
         PLY             ; Byte count
         STY $12         ; Set data size
         LDA #$7E        ; Bank: 7E
         STA $18         ; Set src HB
         RTS

; Set to upload BG3 tilemap A (version 2)
C30F61:  LDY #$4000      ; VRAM: $4000
         LDX #$7849      ; Src: 7E/7849
         BRA C30F7_      ; Set DMA vars

; Set to upload BG3 tilemap B (version 2)
C30F75:  LDY #$4400      ; VRAM: $4400
         LDX #$8049      ; Src: 7E/8049
C30F7_:  PEA $0800       ; Bytes: 2048
         JMP C3A60Y      ; Set DMA vars

; Disable conditional upload during NMI
C30F89:  STZ $1B         ; Null dst LB
         STZ $1C         ; Null dst HB
         RTS

; Load palette from bank C2
pal_c2:  LDA #$C2        ; Src bank: C2

; Load specified palette
C30F8E:  PEA $001F       ; Colors: 16
C30F91:  JSR C35C0_      ; Set src, dst
         LDA #$7E        ; Bank: 7E
         STA $E9         ; Set dst HB
         PLY             ; Color index
C30F9C:  LDA [$EB],Y     ; Color data
         STA [$E7],Y     ; Save in RAM
         DEY             ; One less byte
         BPL C30F9C      ; Loop till last
         RTS

; Set to convert to a palette in C2, slowness = 4
c2slw4:  PEA $04C2       ; Src: C2, Spd: 4
         BRA set_ed      ; Branch below

; Set to convert to a palette in C2, slowness = 2
c2slw2:  PEA $02C2       ; Src: C2, Spd: 2
         BRA set_ed      ; Skip 2 lines

; Set to convert to black palette, slowness = 4
black4:  PEA $04C3       ; Src: C3, Spd: 4
         LDX #C310F4     ; Black pal loc
set_ed:  PLA             ; Src bank
         STA $ED         ; Memorize it
         BRA C30FAB      ; Skip a line

; Set to gradually convert one palette into another
C30FAA:  PHA             ; Save slowness
C30FAB:  PHX             ; Save src LBs
         PHY             ; Save dst LBs
         LDY #C30FDD     ; C3/0FDD
         JSR qanim0      ; Queue fade fn
         LDA $ED         ; Source HB
         XBA             ; Put in B
         LDA #$7E        ; Dest HB
         REP #$20        ; 16-bit A
         STA $7E36C9,X   ; Put in queue
         PLA             ; Dest LBs
         STA $7E33C9,X   ; Put in queue
         PLA             ; Source LBs
         STA $7E3449,X   ; Put in queue
         SEP #$20        ; 8-bit A
         PLA             ; Fade slowness
         STA $7E37CA,X   ; Put in queue
         RTS

; Gradually convert one palette into another
C30FDD:  BNE C30FF6      ; If mode 1

; Mode 0: Initialize timer
C30FE5:  LDA #$1F        ; Frames: 31
         STA $3349,X     ; Set fade timer
         INC $3649,X     ; Mode +1
         SEC             ; Set to requeue
         RTS

; Mode 1: Modify palette
C30FF6:  LDA $35C9,X     ; Delay over?
         BEQ C30FFD      ; Recolor if so
C31032:  DEC $35C9,X     ; Fade delay -1
         SEC             ; Set to requeue
         RTS

; Fork: Load parameters
C30FFD:  LDA $37CA,X     ; Slowness
         STA $35C9,X     ; Set delay
         LDA $36C9,X     ; Dest HB
         STA $E8         ; Memorize it
         LDA $36CA,X     ; Source HB
         STA $EB         ; Memorize it
         REP #$20        ; 16-bit A
         LDA $33C9,X     ; Dest LBs
         STA $E6         ; Memorize them
         LDA $3449,X     ; Source LBs
         STA $E9         ; Memorize them
         LDA $3349,X     ; Fade timer
         STA $F1         ; Memorize it

; Fork: Partially convert palette
C31037:  LDY #$001E      ; Color: 16th
C3103C:  LDA [$E9],Y     ; Target color
         JSR C341F_      ; Separate RGB
         LDX #$0002      ; First: Green
         PHX             ; Save index
.copy    LDA $E0,X       ; RGB component
         STA $E3,X       ; Relocate it
         DEX             ; Comp index -1
         BPL .copy       ; Loop till last
         REP #$20        ; 16-bit A
         LDA [$E6],Y     ; Actual color
         JSR C341F_      ; Separate RGB
         PLX             ; First: Green
.cloop   LDA $E3,X       ; Target component
         SEC             ; Prepare SBC
         SBC $E0,X       ; Matches current?
         BEQ .comp       ; Skip if so
         BCC .down       ; -1 if lesser
         CMP $F1         ; Diff < timer?
         BCC .comp       ; Skip if so
         INC $E0,X       ; Component +1
         BRA .comp       ; Skip a line
.down    DEC $E0,X       ; Component -1
.comp    DEX             ; Comp index -1
         BPL .cloop      ; Loop till last
         JSR C34221      ; Merge RGB
         STA [$E6],Y     ; Update color
         DEY             ; Color index -1
         DEY             ; Color index -1
         BPL C3103C      ; Loop till last
         SEP #$20        ; 8-bit A
         JSR C3CFE_      ; Fade timer -1
         BNE C31032      ; Branch if > 0
         CLC             ; Set to dequeue
         RTS

; White palette for an ending transition
C310D4:  dw       $7FFF,$7FFF,$7FFF,$7FFF,$7FFF,$7FFF,$7FFF
         dw $7FFF,$7FFF,$7FFF,$7FFF,$7FFF,$7FFF,$7FFF,$7FFF

; Black palette for most ending transitions
C310F4:  dw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
         dw $0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000

; Reset OAM and animation queue
C31114:  JSR db7e        ; Set DB to 7E
C3114E:  LDX #$01FC      ; Lo-OAM index
C31150:  LDA #$E001      ; X=1, Y=224
         STA $0300,X     ; Hide tile
         TDC             ; Clear A
         INC A           ; Tile num: 1
         STA $0302,X     ; Set attribs
         DEX             ; OAM index -1
         DEX             ; OAM index -1
         DEX             ; OAM index -1
         DEX             ; OAM index -1
         BPL C31150      ; Loop till < 0
         SEP #$20        ; 8-bit A
         LDX #$001F      ; Hi-OAM index
C31168:  STZ $0500,X     ; Clear Hi-OAM
         DEX             ; Index -1
         BPL C31168      ; Loop till < 0
         TDC             ; Zero A
         PHA             ; Put on stack
         PLB             ; Set DB to 0
         TAX             ; Clear X
         STX $2D         ; Queue index: 0
         LDX #$3249      ; 7E/3249
         LDY #$0580      ; Bytes: 1408
         JMP C3A79_      ; Clear queue

; Queue animation function in slot 0 or further
qanim0:  SEP #$20        ; 8-bit A
         TDC             ; Min slot: 0
         BRA C31173      ; Queue anim fn

; Queue animation function in slot 8 or further
qanim1:  LDA #$01        ; Min slot: 8
         BRA C31173      ; Queue anim fn

; Queue animation function in slot 16 or further
qanim2:  LDA #$02        ; Min slot: 16
         BRA C31173      ; Queue anim fn

; Queue animation function in slot 24 or further
qanim3:  LDA #$03        ; Min slot: 24

; Queue animation function
C31173:  PHA             ; Save slot
         TDC             ; Clear A
         PLA             ; Starting slot
         JSR x32r        ; x32
         LSR A           ; x16
         TAX             ; Index it
C31196:  LDA $7E3249,X   ; Empty slot?
         BNE C311A0      ; Skip if not
         TYA             ; Animation fn
         STA $7E3249,X   ; Put in queue
         SEP #$20        ; 8-bit A
;        LDA $2D         ; Caller's queue
;        STA $7E374A,X   ; Put in queue
         TDC             ; Clear A
         RTS

; Fork: Try next slot
C311A0:  INX             ; Index +1
         INX             ; Index +1
         CPX #$0080      ; Past slot 64?
         BNE C31196      ; Loop if not
         STP             ; Freeze game

; Process animation queue, hide inactive objects
C311B0:  JSR db7e_       ; Set DB to 7E
         LDX #$0300      ; 7E/0300
         STX $0E         ; Set Lo-OAM ptr
         LDX #$0500      ; 7E/0500
         STX $10         ; Set Hi-OAM ptr
         LDX #$0003      ; Hi-OAM index: 3
         STX $33         ; Hi-OAM: 4x 8x8
         LDX #$0080      ; Objects: 128
         STX $31         ; Set space left
         LDX $00         ; Queue index: 0
C311CE:  LDY $3249,X     ; Animation fn
         BEQ C311F5      ; Skip if blank
         STX $2D         ; Set queue index
         PHX             ; Save index
         STY $E0         ; Set anim fn
         SEP #$20        ; 8-bit A
         TDC             ; Clear A
         TXY             ; Put index in Y
         LDA $3649,X     ; Anim mode
         ASL A           ; Double it
         JSR C31203      ; Run anim fn
         REP #$20        ; 16-bit A
         PLX             ; Queue index
         BCS C311F5      ; Branch if kept
         STZ $3249,X     ; Anim fn: None
         STZ $3649,X     ; Anim mode: 0
         STZ $35C9,X     ; Misc. specs: 0
         STZ $3749,X     ; OAM complements
C311F5:  INX             ; Index +1
         INX             ; Index +1
         CPX #$0080      ; Done 64 slots?
         BNE C311CE      ; Loop if not

; Fork: Hide inactive tiles in OAM
C31338:  LDY $31         ; Unused tiles
         BEQ C3134C      ; Exit if none
         REP #$20        ; 16-bit A
         LDX #$01FC      ; Tile: Last
         LDA #$E001      ; X=1, Y=224
C31342:  STA $0300,X     ; Hide tile
         DEX             ; OAM index -1
         DEX             ; OAM index -1
         DEX             ; OAM index -1
         DEX             ; OAM index -1
         DEY             ; Tiles left -1
         BNE C31342      ; Loop till last
C3134C:  SEP #$20        ; 8-bit A
         PLB             ; Restore DB
         RTS

; Execute queued animation function
C31203:  JMP ($00E0)     ; Run anim fn

; Initialize M7 or object's pose number and timer
C3120_:  SEP #$20        ; 8-bit A
         INC $3649,X     ; Mode +1
C31206:  SEP #$20        ; 8-bit A
         STZ $36C9,X     ; Pose: 1st
         LDY $32C9,X     ; Anim ptr LBs
         INY             ; Add 2 to skip..
         INY             ; ..pose pointer
         STY $EB         ; Set src LBs
         LDA $35CA,X     ; Anim ptr HB
         STA $ED         ; Set src HB
         LDA [$EB]       ; Pose timer
         STA $36CA,X     ; Set sprite's
         RTS

; Handle object's pose timer, then define OAM
C31221:  JSR C31227      ; Pose timer -1

; Fork: Define OAM for object's tiles
C31273:  JSR ld36C9      ; Pose OAM ptr
         JSR E7_35CA     ; Set source
         LDA $31         ; OAM's full?
         BEQ C312FB      ; Exit if so
         LDA [$E7],Y     ; Tile count
         STA $E6         ; Memorize it
         BEQ C312FB      ; Exit if none

; Fork: Large tile (16x16)
C31296:  INY             ; Index +1
         LDA [$E7],Y     ; Tile X & flag
         PHA             ; Memorize it
         BPL C312B0      ; Branch if 8x8
         PLA             ; Unstack this
         ASL A           ; Clear flag
         LSR A           ; Restore X; C-
         PHA             ; Save X coord
         TDC             ; Clear A
         LDA $33         ; Hi-OAM index
         TAX             ; Index it
         LDA.L C31334,X  ; Size flag
         TSB $34         ; Add to 4-pack
         LDX $2D         ; Queue index

; Fork: Small tile (8x8)
C312B0:  LDA $34         ; OAM flag pack
         STA ($10)       ; Put in Hi-OAM

; Fork: Pan with BG1
C312BA:  LDA $364A,X     ; Sprite flags
         LSR A           ; Pan with BG1?
         BCC C312CE      ; Branch if not
         PLA             ; Tile's rel X
         SBC $35         ; Deduct BG1 X-Pos
         PHA             ; Save changes

; Fork: Adjust Hi-OAM index
C312CE:  DEC $33         ; Hi-OAM index -1
         BPL C312DF      ; Branch if 0+
         LDA #$03        ; Tile: 1 of 4
         STA $33         ; Reset size index
         STZ $34         ; Hi-OAM: 4x 8x8
         INC $10         ; Hi-OAM ptr +1

; Fork: Set tile's position
C312DF:  PLA             ; Tile's rel X
         CLC             ; Prepare ADC
         ADC $33CA,X     ; Add object's X
         STA ($0E)       ; Save in OAM
         INC $0E         ; Lo-OAM ptr +1
         INY             ; Index +1
         LDA [$E7],Y     ; Tile's rel Y
         CLC             ; Prepare ADC
         ADC $344A,X     ; Add object's Y
         STA ($0E)       ; Save in OAM
         INC $0E         ; Lo-OAM ptr +1

; Fork: Set tile's attributes
         INY             ; Index +1
         LDA [$E7],Y     ; Tile num LSBs
         STA ($0E)       ; Save in OAM
         INC $0E         ; Lo-OAM ptr +1
         INY             ; Index +1
         LDA $364A,X     ; Sprite flags
         AND #$02        ; Mirror H-wise?
         BEQ C31320      ; Branch if not
         LDA #$40        ; H-Flip flag
C31320:  ORA [$E7],Y     ; Add attributes
         STA ($0E)       ; Save in OAM
         LDA $3749,X     ; OAM complements
         BEQ C31332      ; Branch if none
         LDA ($0E)       ; Tile attributes
         AND #$F1        ; Clear palette
         ORA $3749,X     ; Add complements
         STA ($0E)       ; Save changes

; Fork: Handle next tile
C31332:  REP #$20        ; 16-bit A
         INC $0E         ; Lo-OAM ptr +1
         SEP #$20        ; 8-bit A
         DEC $31         ; Space left -1
         BEQ C312FB      ; Exit if full
         DEC $E6         ; Tiles left -1
         BNE C31296      ; Loop till last
C312FB:  RTS

; Hi-OAM complements
C31334:  db $80          ; Tile 4: Large
         db $20          ; Tile 3: Large
         db $08          ; Tile 2: Large
         db $02          ; Tile 1: Large

; Handle pose or Mode 7 timer
C31227:  LDX $2D         ; Queue index
         LDY $32C9,X     ; Anim ptr LBs
         STY $EB         ; Set src LBs
         SEP #$20        ; 8-bit A
         LDA $35CA,X     ; Anim ptr HB
         STA $ED         ; Set src HB

; Fork: Check if expired
C3124C:  LDA $36CA,X     ; Timer expired?
         BNE C31239      ; Branch if not
         LDA $36C9,X     ; Pose index
         CLC             ; Prepare ADC
         ADC #$03        ; Point to next
         BRA C31244      ; Set new timer

; Fork: Handle control codes
C31239:  CMP #$FE        ; Infinite?
         BEQ C31262      ; Exit if so
         BCC C3125F      ; If not loop point
         TDC             ; Pose: 1st
C31244:  STA $36C9,X     ; Set pose index

; Fork: Initialize timer
C31263:  TDC             ; Clear A
         LDA $36C9,X     ; Pose index
         TAY             ; Index it
         INY             ; Skip ptr LB
         INY             ; Skip ptr HB
         LDA [$EB],Y     ; Pose timer
         STA $36CA,X     ; Set object's
         BRA C3124C      ; Repeat checks

; Fork: Decrease timer
C3125F:  DEC $36CA,X     ; Timer -1
C31262:  RTS

; Trigger NMI, decode joypads, etc.
C3134D:  JSR C31368      ; Trigger NMI
         JSR C3A48_      ; Decode joypad
         JSR C3A4BD      ; Handle autofire
         TDC             ; Clear A
         TAY             ; Clear Y
         LDA $20         ; Transition timer
         BEQ C31359      ; Branch if off
         STY $08         ; No-autofire keys
         STY $0A         ; Semi-auto keys
C31359:  LDA $46         ; Menu flags
         BIT #$20        ; Scroll list?
         BEQ C31262      ; Exit if not

; Fork: Adjust BG1 V-Shift amounts while list scrolls
C38105:  LDX $00         ; Scanline: 88th
C38108:  LDA $7E985B,X   ; Scanline amount
         CMP #$1E        ; Last 30 lines?
         BEQ C31262      ; Exit if so
         TXY             ; RAM index
         LDA $20         ; Scroll timer
         XBA             ; Put in B
         LDA #$06        ; Multiplier
         JSR mul8        ; Timer x6
         LDX $41         ; Scrolling up?
         BPL C38123      ; Branch if not
         ADC #$12        ; Advance index
C38123:  TAX             ; Index it
         LDA #$03        ; Loop counter
.loop    PHA             ; Memorize it
         REP #$21        ; 16-bit A; C-
         LDA.L C3815F,X  ; Shift mod
         INX             ; ROM index +1
         INX             ; ROM index +1
         PHX             ; Save ROM index
         TYX             ; RAM index
         ADC $7E985C,X   ; Add actual shift
         STA $7E985C,X   ; Set actual shift
         PLX             ; ROM index
         INY             ; RAM index +1
         INY             ; RAM index +1
         INY             ; RAM index +1
         SEP #$20        ; 8-bit A
         PLA             ; Loop counter
         DEC A           ; Loop counter -1
         BNE .loop       ; Loop till 0
         TYX             ; RAM index
         BRA C38108      ; Loop

; Trigger NMI, allow cursor sound to repeat
C31368:  LDA #$81        ; Stop IRQ timers
         STA $4200       ; On: NMI, joypads
         STA $24         ; Mark NMI request
         CLI             ; Unmask IRQs
C31370:  LDA $24         ; Back from NMI?
         BNE C31370      ; Loop if not
         SEI             ; Mask IRQs
         LDA $44         ; Brightness
         STA $2100       ; Apply it now
         LDA $43         ; Queued HDMA
         STA $420C       ; Update channels
         LDA $B5         ; Mosaic settings
         STA $2106       ; Apply to screen
         STZ $AE         ; Allow SFX repeat
         TDC             ; Clear A
         RTS

; Handle NMI (menu, ending)
C31387:  REP #$30        ; 16-bit A, X, Y
         PHA             ; Save A
         PHX             ; Save X
         PHY             ; Save Y
         PHB             ; Save DB
         PHD             ; Save DP
         SEP #$20        ; 8-bit A
         LDA $4210       ; Clear NMI flag
         PEI ($00)       ; Push #$0000
         PLD             ; Set DP to 0
         TDC             ; Clear A
         PHA             ; Put on stack
         PLB             ; Set DB to 0
         JSL $C00015     ; Event timers -1
         JSL C313C8      ; Play time +1
         INC $23         ; NMI count +1
         LDA $24         ; Manual NMI?
         STZ $24         ; Unmark such
         BEQ C313B1      ; If not manual
         JSR C31412      ; Refresh screen
         REP #$20        ; 16-bit A
         INC $CF         ; Ending timer +1
         LDA $20         ; Sequence timer
         BEQ C313B1      ; Ignore if off
         DEC $20         ; Timer -1
C313B1:  REP #$30        ; 16-bit A, X, Y
         PLD             ; Restore DP
         PLB             ; Restore DB
         PLY             ; Restore Y
         PLX             ; Restore X
         PLA             ; Restore A
C313C6:  RTI

; Advance play time (NMI)
C313C8:  LDA $021E       ; Frames
         CMP #$3C        ; 60?
         BNE C313FC      ; +1 if not

; Fork: Seconds +1
C313D1:  STZ $021E       ; Frames = 0
         LDA #$3B        ; Value: 59
         CMP $021D       ; Seconds = 59?
         BEQ C313E0      ; +1 min if so
         INC $021D       ; +1 second
         BRA C313FC      ; +1 frame

; Fork: Minutes +1
C313E0:  STZ $021D       ; Seconds = 0
         CMP $021C       ; Minutes = 59?
         BEQ C313EF      ; +1 hour if so
         INC $021C       ; +1 minute
         BRA C313FC      ; +1 frame

; Fork: Hours +1
C313EF:  STZ $021C       ; Minutes = 0
         LDA $021B       ; Hours
         CMP #$63        ; 99?
         BEQ C313FC      ; Branch if so
         INC $021B       ; +1 hour

; Fork: Frames +1
C313FC:  LDX $021B       ; Hours and mins
         CPX #$3B63      ; 99:59?
         BNE C3140D      ; Branch if not
         STZ $021D       ; Seconds = 0
C3140D:  INC $021E       ; +1 frame
         RTL

; Refresh screen (NMI)
C31412:  STZ $420C       ; Disable HDMA
         LDX $00         ; Src index: 0
         TXY             ; Dst index: 0
.loop    LDA $35,X       ; BG X or Y LB
         STA $210D,Y     ; Apply it now
         LDA $36,X       ; BG X or Y HB
         STA $210D,Y     ; Apply it now
         LDA $B7,X       ; Matrix or..
         STA $211B,Y     ; ..center X/Y
         LDA $B8,X       ; HB of above
         STA $211B,Y     ; Apply it now
         INX             ; Src index +1
         INX             ; Src index +1
         INY             ; Dst index +1
         CPY #$0006      ; Past range?
         BNE .loop       ; Loop if not
         JSR C31463      ; Refresh OAM
         JSR C314D2      ; Refresh CGRAM
         JSR C31488      ; Do VRAM DMA A

; Handle conditional VRAM transfer (NMI)
C314AC:  LDY $1B         ; Destination
         BEQ C314FD      ; Exit if none
         STY $2116       ; Set VRAM ptr
         LDY $19         ; Number of bytes
         LDX $1D         ; Source LBs
         LDA $1F         ; Source HB
dmast1:  PEA $1801       ; 2Rx1B to VRAM
         BRA dmast2      ; Move data

; Refresh OAM (NMI)
C31463:  TDC             ; Src bank: 00
         TAX             ; $0000
         STX $2102       ; Set OAM address
         LDY #$0220      ; Bytes: 544
         LDX #$0300      ; Src: 00/0300
         PEA $0402       ; 1Rx2B to OAM
         BRA dmast2      ; Move data

; Handle absolute VRAM transfer (NMI)
C31488:  LDY $14         ; Destination
         STY $2116       ; Set VRAM ptr
         LDY $12         ; Number of bytes
         LDX $16         ; Source LBs
         LDA $18         ; Source HB
         BRA dmast1      ; Move data

; Refresh CGRAM (NMI)
C314D2:  LDA $45         ; Menu flags
         LSR A           ; Freeze CGRAM?
         BCC C314FD      ; Exit if so
         STZ $2121       ; To BG color 0
         LDY #$0200      ; Bytes: 512
         LDX #$3049      ; Src: 7E/3049
         PEA $2202       ; 1Rx2B to CGRAM
         LDA #$7E        ; Src bank
dmast2:  STX $4302       ; Set src LBs
         STA $4304       ; Set src HB
         PLX             ; DMA mode..
         STX $4300       ; ..and dest
         STY $4305       ; Set data size
         LDA #$01        ; Channel: 0
         STA $420B       ; Move data
C314FD:  RTS

; Upload BG3 tilemaps C and D
C30E7C:  LDY #$4800      ; VRAM: $4800
         LDX #$8849      ; Src: 7E/8849
C30E85:  STY $2116       ; Set VRAM ptr
         LDY #$1000      ; Bytes: 4096
         LDA #$7E        ; Src bank: 7E
         BRA dmast1      ; Move data

; Upload BG1 tilemaps A and B
C30E28:  LDY $00         ; VRAM: $0000
         LDX #$3849      ; Src: 7E/3849
         BRA C30E85      ; Move data

; Upload BG1 tilemaps C and D
C30E44:  LDY #$0800      ; VRAM: $0800
         LDX #$4849      ; Src: 7E/4849
         BRA C30E85      ; Move data

; Upload BG2 tilemaps A and B
C30E52:  LDY #$1000      ; VRAM: $1000
         LDX #$5849      ; Src: 7E/5849
         BRA C30E85      ; Move data

; Upload BG2 tilemaps C and D
C30E60:  LDY #$1800      ; VRAM: $1800
         LDX #$6849      ; Src: 7E/6849
         BRA C30E85      ; Move data

; Upload BG3 tilemaps A and B
C30E6E:  LDY #$4000      ; VRAM: $4000
         LDX #$7849      ; Src: 7E/7849
         BRA C30E85      ; Move data

; Load save data on defeat
C314FE:  LDA $021F       ; File number
         BEQ C31514      ; Reset if none
         JSR C31566      ; Load save file
         JSR C319D1      ; Compute checksum
         JSR C319EB      ; Bad checksum?
         BEQ C31514      ; Reset game if so
         JSR C31595      ; Move timer data
         TDC             ; Reset game: No
         BRA C31518      ; Set aftereffect

; Fork: Reset game
C31514:  LDA #$FF        ; Reset game: Yes
C31518:  STA $0205       ; Set aftereffect
         RTL

; Load save file
C31566:  JSR srm_xy      ; Define X and Y
C31577:  LDA $306000,X   ; Saved data
         STA $1600,Y     ; Copy to RAM
         DEX             ; SRAM index -1
         DEY             ; RAM index -1
         BPL C31577      ; Loop till < 0
         RTS

; Define indices for loading or saving game file
srm_xy:  LDY #$0A00      ; File size
         JSR mul16       ; Times file number
         DEX             ; Product -1
         DEY             ; First: 7E/1FFF
         RTS

; Restore saved event timer data
C31595:  LDX #$0017      ; Data index
C31597:  LDA $1FA8,X     ; Timer data
         STA $1188,X     ; Make active
         DEX             ; Index -1
         BPL C31597      ; Loop till < 0
         RTS

; Output all save files
C31632:  LDA #$90        ; Object color 16
         STA $2121       ; CGRAM: $0120
         LDA #$20        ; Colors: 16
         LDX #$E8A0      ; Src: D8/E8A0
         LDY #$3169      ; Dst: 7E/3169
         JSR C36BEE      ; Load grayscale
         LDA #$02        ; Y: 2
         STA $37         ; Set BG1 Y-Pos
         REP #$20        ; 16-bit A
         LDA #$7FFF      ; Font: White
         JSR C33F9E      ; Put in palettes
         LDA #$01        ; Gradient set: 1
         JSL $D4CA1D     ; Create gradient
         JSR C36A15      ; Clear BG1 map A
         JSR bg3c_cl     ; Clear BG3 maps
         JSR C318AC      ; Move game's SRAM

; Fork: Load save file, validate checksum
C31663:  TDC             ; File index: 0
C31665:  PHA             ; Memorize it
         PHA             ; Do it again
         INC A           ; File number: 1
         JSR C31566      ; Load save file
         JSR C36915      ; Move party info
         JSR C319D1      ; Compute checksum
         JSR C319EB      ; Test validity
         TDC             ; Clear A
         PLA             ; File index
         ASL A           ; Double it
         TAX             ; Index it
         PHX             ; Memorize it
         PHX             ; Do it again
         PHX             ; Do it again
         JSR C38085      ; Font: White
         STY $91,X       ; Save checksum
         TYA             ; Invalid?
         BEQ C31682      ; Blank if so

; Fork: Draw file contents
C31677:  JSR C36F61      ; List char sprites
         JSR C3171D      ; Draw file text
         BRA C31692      ; Load skin visuals

; Fork: Blank file
C31682:  JSR C318C1      ; Restore skin 1
         PLX             ; File index
         JSR C36F84      ; Use blank party
         REP #$20        ; 16-bit A
         LDA.L emptysv,X ; Text pointer
         TAY             ; Put it in Y
         PHX             ; Save file index
         JSR C302F9      ; Draw "Empty"

; Fork: Load window data
C31692:  PLX             ; File index
         JSR C33AF5      ; Load skin 1 pal
         PLX             ; File index
         JSR C33AA1      ; Upload skin
         PLX             ; File index
         REP #$20        ; 16-bit A
         LDA.L sv_win,X  ; Layout ptr
         TAX             ; Put in Y
         SEP #$20        ; 8-bit A
         JSR C30341      ; Draw window

; Fork: Handle next file
C31695:  PLA             ; File number
         INC A           ; Point to next
         CMP #$03        ; Done all 3?
         BNE C31665      ; Loop if not

; Fork: Restore SRAM block
C318C1:  LDX #$09FF      ; 1st: $1FFF
C318C3:  LDA $7EAC8D,X   ; Data backup
         STA $1600,X     ; Restore it
         DEX             ; Index -1
         BPL C318C3      ; Loop if not
         RTS

; Upload skin graphics, load colors and tile attributes
C33A87:  LDY #$7800      ; $7800
         STY $14         ; Set VRAM ptr
         LDA $1D4E       ; Skin, etc.
         PHA             ; Memorize it
         JSR C33B30      ; Set source
         PLA             ; Skin, etc.
         LDY #$312B      ; To 7E/312B
         JSR C33B5B      ; Load colors
         LDY #$1C00      ; Palette: 7

; Fork: Define attributes for all window tiles
C30326:  LDX #$0036      ; List index
         REP #$21        ; 16-bit A; C-
         TYA             ; Tile attributes
         ADC #$019B      ; Base tile number
C3032C:  STA $7E9F19,X   ; Set current tile
         DEC A           ; Tile number -1
         DEX             ; List index -1
         DEX             ; List index -1
         BPL C3032C      ; Loop till done
         SEP #$20        ; 8-bit A
         RTS

; Upload skin for viewed save file
C331A8:  TDC             ; Clear A
         LDA $66         ; File viewed
         DEC A           ; Now 0-2
         ASL A           ; Double it
         TAX             ; Index it

; Upload skin for save file
C33AA1:  REP #$20        ; 16-bit A
         LDA.L sv_vrm,X  ; VRAM ptr
         STA $14         ; Set dst
         LDA.L sv_atr,X  ; Tile attributes
         PHA             ; Memorize them
         LDA $91,X       ; Blank file?
         BEQ C33AB1      ; Skin 1 if so
         LDA $1D4E       ; Skin, etc.
C33AB1:  SEP #$20        ; 8-bit A
         JSR C33B30      ; Set source
         JSR C31488      ; Upload skin
         PLY             ; Attributes
         BRA C30326      ; Build skin map

; VRAM pointers for save file windows
sv_vrm:  dw $7A00        ; File A
         dw $7C00        ; File B
         dw $7E00        ; File C

; Tile attribute modifers for save file windows
sv_atr:  dw $1820        ; A: Tile+32, pal 6
         dw $1440        ; B: Tile+64, pal 5
         dw $1060        ; C: Tile+96, pal 4

; Load skin colors for save file
C33AF5:  REP #$20        ; 16-bit A
         LDA.L pal_sv,X  ; Dst pal loc
         TAY             ; Put it in Y
         LDA $91,X       ; Blank file?
         BEQ C33B00      ; Skin 1 if so
         LDA $1D4E       ; Skin, etc.
C33B00:  SEP #$20        ; 8-bit A
         JSR C33B5B      ; Load colors
         JMP C314D2      ; Refresh CGRAM

; RAM address for storing skin palette
pal_sv:  dw $310B        ; File A
         dw $30EB        ; File B
         dw $30CB        ; File C

; Blank party shown on save file
C36F84:  LDA #$FF        ; Actor: None
         STA $69         ; Set member A
         STA $6A         ; Set member B
         STA $6B         ; Set member C
         STA $6C         ; Set member D

; List actor sprites for save file
C36F61:  PHX             ; Save file index
         LDX #$0003      ; Member slot: 4
         TDC             ; Clear A
C36F67:  LDA $69,X       ; Actor in slot
         BMI C36F75      ; Blank if none
C36F97:  PHX             ; Save slot
         TXA             ; Member slot
         ASL A           ; Double it
         TAY             ; Index it
         LDX $6D,Y       ; Actor's address
         LDA $01,X       ; Graphics set
         PLX             ; Member slot
C36FA6:  CMP #$0E        ; Soldier sprite?
         BNE C36F75      ; Branch if not
         LDA $1EA0       ; Event flags
         BIT #$08        ; Soldier disguise?
         BEQ C36FB5      ; Pick brown if not
         LDA #$16        ; Use green soldier
         BRA C36F75      ; Skip a line
C36FB5:  LDA #$0E        ; Use brown soldier
C36F75:  PHX             ; Save member slot
         PHA             ; Save sprite set
         TXA             ; Member slot
         CLC             ; Prepare ADC
         ADC $04,S       ; Add file x2
         ADC $04,S       ; Add file x2
         TAX             ; Index sum
         PLA             ; Sprite set
         STA $7EAA71,X   ; Set member's
         PLX             ; Member slot
         DEX             ; Member slot -1
         BPL C36F67      ; Loop till last
         PLX             ; File index
         RTS

; Draw all text for save file
C3171D:  REP #$20        ; 16-bit A
         LDY #$000A      ; Parameters: 10
.loop    LDA.L ldr_tbl,X ; Ptr or position
         PHA             ; Memorize it
         TXA             ; Parameter index
         CLC             ; Prepare ADC
         ADC #$0006      ; Point to next
         TAX             ; Reindex it
         DEY             ; One less left
         BNE .loop       ; Loop till last

; Fork: Draw time
         PLY             ; Text pointer
         JSR C302F9      ; Draw ":"
         LDA $1863       ; Hours played
         PLX             ; Text position
         JSR C304B_      ; Draw hours
         JSR C304F_      ; Convert minutes
         PLX             ; Text position
         JSR C304B6      ; Draw minutes

; Fork: Draw white actor info
C3174D:  TDC             ; Clear A
         TAX             ; 1st: Member 1
C3174F:  LDA $69,X       ; Actor in slot
         BMI C3177C      ; Skip if empty
C317BE:  TXA             ; Member slot
         JSR C34352      ; Actor's address
         PLY             ; Text pointer
         JSR C302F9      ; Draw "/"
C3177F:  LDX $67         ; Actor's address
         LDA $08,X       ; Level
         PLX             ; Text position
         JSR C304B_      ; Draw 2 digits
         LDY $67         ; Actor's address
         JSR C32B9A      ; Truncate HP
         JSR hpmptxt     ; Convert HP
         PLX             ; Text position
         JSR C30490      ; Draw 4 digits
         JSR boosth_     ; Define max HP
         JSR C3052E      ; Turn into text
         PLX             ; Text position
         JSR C30490      ; Draw 4 digits
         PLY             ; Text position
         JSR C334CF      ; Draw actor name

; Fork: Draw blue text
         PLY             ; Text pointer
         JSR draw2c      ; Draw "LV"
         PLY             ; Text pointer
         JMP draw2c      ; Draw "Time"

; Fork: Try next member
C3177C:  INX             ; Member slot +1
         BRA C3174F      ; Try next member

; Back up SRAM block
C318AC:  LDX #$09FF      ; 1st: $1FFF
C318B4:  LDA $1600,X     ; SRAM data
         STA $7EAC8D,X   ; Make backup
         DEX             ; Index -1
         BPL C318B4      ; Loop if not
         RTS

; Display actor's sprite on save file
C319C7:  JSR C31206      ; Set pose timer
C319CC:  JSR C31221      ; Define OAM
         CLC             ; Set to dequeue
         RTS

; Compute SRAM checksum
C319D1:  LDX #$09FD      ; SRAM index
         TDC             ; Checksum: 0
C319D8:  CLC             ; Prepare ADC
         ADC $1600,X     ; Add SRAM byte
         BCC C319E4      ; If no overflow
         XBA             ; Checksum HB
         INC A           ; Add one
         XBA             ; Checksum LB
C319E4:  DEX             ; SRAM index -1
         BPL C319D8      ; Loop till < 0
         TAY             ; Put sum in Y
         RTS

; Validate computed SRAM checksum (buggy if zero)
C319EB:  CPY $1FFE       ; Sum in save file
         BEQ C319F7      ; Exit if identical
         LDY $00         ; Clear checksum
C319F7:  TYX             ; Z-Flag = Validity
         RTS

; 53: Queue fade-out for save menu
C319FB:  JSR C3304F      ; Queue fade-out
         BRA C31A0D      ; Skip a line

; 52: Queue fade-in for save menu
C31A0A:  JSR C33056      ; Queue fade-in
C31A0D:  LDA #$54        ; C3/1A19
         STA $26         ; Next: Wait
         LDY #$0008      ; Frames: 8
         STY $20         ; Set fade timer
         BRA C31903      ; Create actors

; 54: Wait while save menu fades in or out
C31A19:  JSR C31D8B      ; Handle timer

; Create actor sprites for Save or Load menu
C31903:  JSR db7e_       ; Set DB to 7E
         LDX #$000B      ; Actor slot: 12
C31905:  LDA $AA71,X     ; Empty slot?
         BMI C31977      ; Skip if so
         PHX             ; Save slot
         PHX             ; Save slot
         LDY #C319C7     ; C3/19C7
         JSR qanim3      ; Queue OAM fn
         TXY             ; Queue index
         LDA $66         ; Cursor slot
         XBA             ; Put in B
         LDA #$0C        ; Multiplier
         JSR mul8        ; Cursor slot x12
         ADC $01,S       ; Add actor slot
         TAX             ; Index sum
         LDA.L C31997,X  ; OAM complements
         STA $3749,Y     ; Set sprite's
         PLX             ; Actor slot
         LDA.L C3197F,X  ; X position
         STA $33CA,Y     ; Set sprite's
         LDA.L C3198B,X  ; Y position
         STA $344A,Y     ; Set sprite's
         LDA $AA71,X     ; Graphics set
         JSR C378FA      ; Set anim ptr
         JSR D8_35CA     ; Ptr HB: D8
         PLX             ; Actor slot
C31977:  DEX             ; Actor slot -1
         BPL C31905      ; Loop till < 0
         PLB             ; Restore DB
         RTS

; 64: Initialize Item menu
C31AD6:  JSR C31AE2      ; Init list vars
         JSR C31AFE      ; Draw menu; cursor
         BRA C31B55      ; Branch below

; 12: Return from item usage menu
C31B35:  JSR C31AE2      ; Init list vars
         JSR oldcur      ; Restore cursor pos
         JSR C37D25      ; Relocate cursor
         JSR C37DA3      ; Draw menu; elems
C31B55:  JSR C31B0E      ; Shift & OBJ & Fade

; Queue Item menu choice 1, prepare fade-in
C31B2E:  LDA #$08        ; Queue: Choice 1

; Set queued menu command, prepare fade-in
C31C15:  STA $27         ; Set queued cmd
C33541:  LDA #$01        ; BRT: 1 and ON
         STA $44         ; Set brightness
         JMP C31368      ; Refresh screen

; Initialize item list variables (Item, Colosseum)
C31AE2:  JSR C3352F      ; Reset/Stop stuff
         JSR C31B99      ; Queue desc anim
         JSR C36904      ; Reset BGs' X/Y
;        LDY $00         ; Clear Y
         STY $49         ; List scroll: 0
C31AEF:  LDA #$F5        ; Top row: Slot 246
         STA $5C         ; Set scroll limit
         LDY #$010A      ; Onscreen rows: 10
         STY $5A         ; Onscreen cols: 1
C37D1C:  LDY #C37D2B     ; C3/7D2B
         JMP C305FE      ; Load navig data

; Draw Item menu, position cursor in inventory
C31AFE:  JSR b6_1D4E     ; Cursor = Memory?
         BPL C31B08      ; Branch if not
         JSR C30DAC      ; Restore cursor
C31B08:  JSR C37D25      ; Relocate cursor

; Draw Item menu with inventory; upload element graphics
C37DA3:  JSR bg1_2x1     ; BG1: 64x32 at $00
         LDX #C37E13     ; C3/7E13
         JSR C3034Y      ; Draw windows
         JSR bg3d_cl     ; Clear BG3 maps
         LDY #C38D16     ; Text pointer
         JSR draw2c      ; Draw "Item"
         JSR C38085      ; Color: User's
         LDX #C38D10     ; Text ptrs loc
         JSR drawx3      ; Draw options
         JSR C3A73D      ; Build desc map
         JSR C3A9A9      ; Upload elements
;        JSR C36A19      ; Clear BG1 map B
         JSR C37E0D      ; Draw inventory
         JSR C382F1      ; Desc; item count
         JMP mvmapx5     ; Upload tilemaps

; Load portrait graphics and colors for party
C31BA8:  JSR C36915      ; Get party info
         JSR C36E82      ; Load GFX, colors
         LDA #$05        ; Show cursor: Y
         TSB $45         ; Unfreeze CGRAM
         JMP C314D2      ; Refresh CGRAM

; Initialize variables for Equip menu
C31BBD:  JSR C3352F      ; Reset/Stop stuff
         JSR C3B323      ; Relocate cursor
         LDA #$06        ; Main cursor: On
         TSB $46         ; Set menu flag
         BRA C39E5_      ; Branch below

; Initialize variables for Relic menu
C39E50:  JSR C3352F      ; Reset/Stop stuff
         JSR C38EBD      ; Relocate cursor
         JSR C38590      ; Reset/Stop desc
         JSR C31B99      ; Queue desc anim
C39E5_:  JSR C36A08      ; Set Win1 bounds
         STZ $49         ; Top BG1 WR row: 1
         JSR C394B6      ; Set to shift text
C307B0:  LDY #C307B8     ; C3/07B8
         JMP qanim1      ; Queue cursor OAM

; 60: Swap actor in Equip menu, retain Equip mode
C31BE5:  JSR C31C01      ; Draw menu, etc.
         JSR C3964F      ; Draw blue "EQUIP"
         LDA #$56        ; C3/9884
         BRA C31BF_      ; Branch below

; 61: Swap actor in Equip menu, retain Remove mode
C31BF3:  JSR C31C01      ; Draw menu, etc.
         JSR C39656      ; Draw blue "REMOVE"
         LDA #$55        ; C3/98CF
C31BF_:  JSR C32AA5      ; Queue slot choice

; Put cursor on first gear slot
C31C0A:  JSR C315B8      ; Load navig data
         JMP C38E75      ; Relocate cursor

; Redraw Equip menu after actor swap
C31C01:  JSR C31BBD      ; Reset variables
         JSR C39032      ; Draw menu; status
         BRA C39614      ; Switch windows

; Initiate text shifting, sprites, fade-in (Item, Colosseum)
C31B0E:  JSR C37E2B      ; Set to shift text
         JSR C307B0      ; Queue cursor OAM
         JSR C3BC02      ; Create scrollbar
         BRA C39EAE      ; Next: Fade-in

; 62: Swap actor in Relic menu, retain Equip mode
C39E7D:  JSR C39E99      ; Draw menu, etc.
         JSR C3964F      ; Draw blue "EQUIP"
         LDA #$5A        ; C3/9FFD7
         BRA C39E8_      ; Branch below

; 63: Swap actor in Relic menu, retain Remove mode
C39E8B:  JSR C39E99      ; Draw menu, etc.
         JSR C39656      ; Draw blue "REMOVE"
         LDA #$5C        ; C3/A10A
C39E8_:  JSR C31C15      ; Queue: Slot list

; Fork: Put cursor on first relic slot, trigger fade-in
C39EA8:  JSR C315D4      ; Load navig data
         JSR C38EDE      ; Relocate cursor
C39EAE:  LDA #$01        ; C3/1D7E
         STA $26         ; Next: Fade-in
         RTS

; Reset Relic menu after pushing L or R in relic slot list
C39E99:  JSR C39E50      ; Init variables
         JSR C3906A      ; Draw menu, etc.
         JSR C39E0F      ; Queue text upload

; Switch to layout without options in Equip or Relic menu
C39614:  LDY #$0100      ; X: 256
C3961_:  STY $39         ; Set BG2 X-Pos
         JSR C3965D      ; Erase options
txt2C:   LDA #$2C        ; Palette 3
         STA $29         ; Color: Blue
         RTS             ; ^ For "Equip"

; Switch to layout with options in Equip or Relic menu
C3960C:  LDY $00         ; BG2 X-Pos: 0
         BRA C3961_      ; Branch above

; 40: Open Equip menu after optimizing gear
C31C1A:  JSR C31BBD      ; Init variables
         JSR C396F0      ; Optimize gear
         BRA C31C2_      ; Draw menu, etc.

; 41: Open Equip menu after removing all gear
C31C26:  JSR C31BBD      ; Init variables
         JSR C396A8      ; Remove gear
C31C2_:  LDA #$02        ; Menu: Equip
         STA $25         ; Set submenu
         BRA C31BD7      ; Draw menu, etc.

; 35: Initialize Equip menu
C31BB8:  JSR C31BBD      ; Init variables

; Draw Equip menu, update status based on gear, trigger fade-in
C31BD7:  JSR C39032      ; Draw menu; status
         LDA #$36        ; C3/9621
C31BE2:  JMP C32AA5      ; Queue: Option list

; 04: Initialize main menu
C31A8A:  JSR C3352Y      ; Reset/Stop stuff
         JSR C36BE8      ; Reset font colors
         JSR C31BA8      ; Load portraits
         JSR C30F89      ; Stop VRAM DMA B
         LDA #$E0        ; Channels: 5-7
         TRB $43         ; Halt HDMA 5-7
         LDX #$F708      ; X: 8-247
         STX $2128       ; Set Win2 pos
         JSR dma2on      ; Queue Win1 HDMA
         LDA #$03        ; 64x64 at $0000
         STA $2107       ; Set BG1 map loc
         LDA #$43        ; 64x64 at $4000
         STA $2109       ; Set BG3 map loc
         JSR C330F5      ; Draw menu
         JSR C31D94      ; Queue cursor fns
         JSR C3354E      ; HDMA: MS desig.
         JSR C36904      ; Reset BGs' X/Y
         INC $37         ; BG1 Y-Pos = 1
         INC $37         ; BG1 Y-Pos = 2
         LDY #C336C2     ; Src: C3/36C2
         JSR C3C01B      ; Set to shift BG3
         LDA #$05        ; C3/1DA4
         BRA C31BE2      ; Queue: Sustain menu

; 19: Initialize Skills menu
C31B5B:  JSR C3352F      ; Reset/Stop stuff
         JSR C32CDF      ; Get actor stats
         JSR C36A08      ; Set Win1 bounds
         STZ $49         ; Top BG1 WR row: 1
         JSR C34DBC      ; Set to shift text
         JSR C34C80      ; Draw menu, etc.
         JSR C34B50      ; Load navig data
         JSR b6_1D4E     ; Cursor = Memory?
         BPL C31B85      ; Branch if not
         JSR ld28x2      ; X = Mbr slot x2
         LDY $0236,X     ; Former position
         STY $4D         ; Set as current
C31B85:  JSR C34B59      ; Relocate cursor
         JSR C307B0      ; Queue cursor OAM
         JSR C31B99      ; Queue desc anim
         LDA #$0A        ; C3/1FF4
         BRA C31BE2      ; Queue: Sustain menu

; 38: Initialize party gear overview
C31C32:  JSR C3352F      ; Reset/Stop stuff
         JSR C39497      ; Set to shift text
         JSR C38EED      ; Draw menu
         LDA #$39        ; C3/2966
C31C3_:  BRA C31BE2      ; Queue: Sustain menu

; 0B: Initialize Status menu
C31C46:  JSR C35D17      ; Boxes, blue text
         JSR C35DC1      ; Draw Gogo's panel
         JSR C35F8D      ; Draw actor info
         JSR C361AC      ; Create portrait
         JSR C3319F      ; Upload tilemaps
         JSR C31C5D      ; Init cursor data
         LDA #$0C        ; C3/21F5
C31C4_:  BRA C31C3_      ; Queue: Sustain menu

; 0D: Initialize Config menu
C31C7D:  JSR C3352F      ; Reset/Stop stuff
         JSR C33A4C      ; Set Win2 bounds
         JSR bg1_1x2     ; BG1: 32x64 at $00
         LDY #$FFFB      ; Y: -5
         STY $3F         ; Set BG3 Y-Pos
         JSR C36A28      ; Clear BG2 map A
         LDX #C339E2     ; C3/39E2
         JSR C3034Y      ; Draw windows
         JSR bg1b_cl     ; Clear BG1 maps
         JSR bg3b_cl     ; Clear BG3 maps
         LDY #C349A1     ; Text pointer
         JSR draw2c      ; Draw "Config"
         JSR txt24       ; Color: Blue
         LDX #C34993     ; Text ptrs loc
         LDA #$0C        ; Strings: 12
         JSR C369BA      ; Draw blue BG1 text
         JSR C38085      ; Color: User's
         LDX #C34907     ; Text ptrs loc
         LDA #$08        ; Strings: 8
         JSR C369BA      ; Draw Fast, etc.
         JSR C33B8C      ; Draw Active, Wait
         JSR C33BB7      ; Draw Bat.Spd nums
         JSR C33BF2      ; Draw Msg.Spd nums
         JSR C33C33      ; Draw Window, Short
         JSR C33C5C      ; Draw On and Off
         JSR C33C85      ; Draw Stereo, Mono
         JSR C33CB0      ; Draw Reset, Memory
         JSR C33CDB      ; Draw Optimum, Empty
         JSR C33D06      ; Draw Single/Multiple
         JSR C33FFD      ; Draw M.Order options
         JSR C340EA      ; Draw skin numbers
         JSR C33F36      ; Draw RGB, palette
         JSR C32406      ; Get nav data; BG1 Y
         JSR mvmapx3     ; Upload tilemaps
         LDY #C33980     ; C3/3980
         JSR qanim1      ; Queue arrow OAM
         LDA $5F         ; Return position
         STA $4E         ; Set cursor row
         JSR C33861      ; Relocate cursor
         JSR C307B0      ; Queue cursor OAM
         LDA #$0F        ; C3/22C5
C31C7_:  BRA C31C4_      ; Queue: Sustain menu

; 15: Initialize save confirmation
C31CE3:  JSR iniconf     ; Call fn below
         JSR C33128      ; Draw menu
         LDA #$16        ; C3/259D
         BRA C31C7_      ; Queue: Sustain menu

; Initialize data shared by confirmation menus
iniconf: JSR C3352F      ; Reset/Stop stuff
         JSR C315D4      ; Load navig data
         JSR C315DD      ; Relocate cursor
         JSR C307B0      ; Queue cursor OAM
C318D1:  LDY #C318F0     ; Src: C3/18F0
         JMP C3C01B      ; Set to shift BG3

; 22: Initialize load confirmation
C31D51:  JSR iniconf     ; Call fn above
         JSR C3314C      ; Draw menu
         LDA #$23        ; C3/B466
         BRA C31C7_      ; Queue: Sustain menu

; Initialize cursor data for Status menu
C31C5D:  JSR C34EDD      ; Actor's address
         LDA #$0C        ; Gogo
         CMP $0000,Y     ; Current actor?
         BNE C31C78      ; Abort if not
         LSR A           ; Main cursor: On
         TSB $46         ; Set menu flag
         JSR C315B8      ; Load navig data
         JSR C33708      ; Relocate cursor
         JMP C307B0      ; Queue cursor OAM
C31C78:  LSR A           ; Main cursor: Off
         TRB $46         ; Set menu flag
         RTS

; 13: Initialize Save menu
C31CA0:  LDY #C315B3     ; C3/15B3
         JSR C305FE      ; Load navig data
         LDY #C31A78     ; Ptr to "Save"
         LDA $0224       ; Last viewed file
         DEC A           ; Now 0-2
         JSR ini_sv      ; Draw menu, etc.
         JSR C315AD      ; Relocate cursor
         LDA $4B         ; Cursor slot
         INC A           ; Now 1-3
         STA $66         ; Set active file
         LDA #$14        ; C3/2540
         BRA C31D4E      ; Queue: Sustain menu

; 20: Initialize Load menu
C31D03:  JSR C315B8      ; Load navig data
         LDA #$02        ; Main cursor: On
         STA $46         ; Bar/Blinker: Off
         LDY #C31A7F     ; Ptr to "New Game"
         LDA $307FF0     ; Last altered file
         JSR ini_sv      ; Draw menu, etc.
         JSR C315C1      ; Relocate cursor
         LDA $4B         ; Cursor slot
         STA $66         ; Set active file
         LDA #$21        ; C3/29C2
C31D4E:  JMP C31C15      ; Queue: Sustain menu

; Initialize data shared by Save and Load menus
ini_sv:  PHA             ; Save old cursor pos
         PHY             ; Save text pointer
         JSR C3352F      ; Reset/Stop stuff
         LDA #$E3        ; CH: 0-1, 5-7
         TRB $43         ; Halt HDMA channels
         JSR C36A28      ; Clear BG2 map A
         JSR C31632      ; Handle files
         JSR C33A87      ; Load skin visuals
         LDX #C3189C     ; C3/189C
         JSR C30341      ; Draw top window
         PLY             ; Text pointer
         JSR draw20      ; Draw text at top
         JSR mvmapx3     ; Upload maps A+B
         JSR C30E7C      ; Upload BG3 C+D
         JSR C36CC7      ; Load actor colors
         JSR C36C84      ; Load cursor colors
         PLA             ; Last file used
         LDY $91         ; File 1 checksum
         BNE .pt1        ; Branch if not 0
         LDY $93         ; File 2 checksum
         BNE .pt1        ; Branch if not 0
         LDY $95         ; File 3 checksum
         BEQ .pt2        ; Branch if 0
.pt1     STA $4E         ; Set cursor row
.pt2     LDA #$52        ; Next: Fade-in
         BRA C31D9_      ; Queue cursor OAM

; 00: Queue fade-out, prepare transition
C31D71:  LDA #$02        ; C3/1D8B
C31D7_:  STA $26         ; Next: Wait
         JSR C3304F      ; Queue fade-out
         BRA C31D85      ; Set fade timer

; 01: Queue fade-in, prepare transition
C31D7E:  LDA #$02        ; C3/1D8B
C31D80:  STA $26         ; Next: Wait
         JSR C33056      ; Queue fade-in
C31D85:  LDY #$0008      ; Frames: 8
         STY $20         ; Set fade timer

; 02: Wait while menu fades in or out
C31D8B:  LDY $20         ; Timer expired?
         BNE C31DE7      ; Exit if not
         JMP C39CDD      ; Get queued cmd

; 03: Exit main menu's portrait area
C31D94:  LDY #C32F42     ; C3/2F42
         JSR qanim0      ; Queue D-Pad fn
         LDA #$05        ; C3/1DA4
C31D9_:  STA $26         ; Next: Main menu
         JMP C307B0      ; Queue cursor OAM

; 05: Sustain main menu
C31DA4:  JSR C33548      ; Redraw time
         LDA $08         ; Pushing A?
         BMI C32E62      ; Branch if so

; Fork: Handle left
C31DB0:  LDA $09         ; No-autofire keys
         BIT #$02        ; Pushing left?
         BEQ C31DBC      ; Branch if not
         JSR C30EA3      ; Sound: Cursor
C32EC6:  LDA #$07        ; List cursor: Off
         TRB $46         ; Blinker: Off
         JSR C32A7Y      ; Set up cursor
         LDY #$5065      ; Pan + Choice 1
         JMP C363FX      ; Init scroll data

; Fork: Handle B
C31DBC:  BPL C31DE7      ; If not pushing B
         STZ $0205       ; Clear field event
         LDA #$FF        ; Queue menu exit
         JSR C3224F      ; SFX + Fade out

; Update field effects from gear (Charms, Sprint Shoes)
C31DD2:  STZ $11DF       ; Clear effects
         LDX #$0003      ; Member slot: 4
C31DD7:  LDA $69,X       ; Actor in slot
         BMI C31DE1      ; Skip if none
         JSR C32CE5      ; Load gear FX
C31DE1:  DEX             ; Slot -1
         BPL C31DD7      ; Loop till last
C31DE7:  RTS

; Handle selection in main menu list
C32E62:  TDC             ; Clear A
         LDA $4B         ; Chosen menu
         STA $25         ; Set submenu
         ASL A           ; Double it
         TAX             ; Index it
         JMP (C32E6C,X)  ; Handle choice

; Jump table for the above
C32E6C:  dw C32E86       ; Item
         dw C32E90       ; Skills
         dw C32E90       ; Equip
         dw C32E90       ; Relic
         dw C32E90       ; Status
         dw C32E7A       ; Config
         dw C32EAA       ; Save

; 06: Handle single actor targeting in main menu
C31DE8:  JSR C33548      ; Redraw time
         LDA $09         ; Pushing B?
         BPL C31DFD      ; Branch if not
         JSR C343C_      ; Cursors: Off
         LDA #$03        ; Next: Main menu
         BRA C31D9_      ; Sound: Cursor

; Fork: Handle MT mode switch
C31DFD:  AND #$02        ; Pushing left?
         BEQ C31E1F      ; Branch if not
         CMP $25         ; Equip command?
         BEQ C31E0D      ; Branch if so
         INC A           ; Point to Relic
         CMP $25         ; Relic command?
         BNE C31E1F      ; Branch if not
C31E0D:  JSR C30EA3      ; Sound: Cursor
         LDA $4E         ; Cursor row
         STA $5E         ; Set return loc
         LDA #$37        ; C3/1EB5
C31E0_:  STA $26         ; Next: Target all
         LDA #$06        ; Main cursor: Off
         TRB $46         ; Set menu flag

; Fork: Create a blinking cursor by every portrait
C3089F:  LDX #$0006      ; Member: Last
C308A1:  LDA $85,X       ; Nobody here?
         BEQ C308C9      ; Skip if so
         PHX             ; Save index
         LDY $85,X       ; Coordinates
         PHY             ; Memorize them
         LDY #C308D1     ; C3/08D1
         JSR qanim1      ; Queue OAM fn
         PLA             ; X position
         JSR C30940      ; Set cursor's
         PLA             ; Y position
         JSR C3EA0Z      ; Set cursor's
         PLX             ; Member index
C308C9:  DEX             ; Index -1
         DEX             ; Index -1
         BPL C308A1      ; Loop till last
C308D0:  RTS

; Fork: Handle A
C31E1F:  LDA $08         ; Pushing A?
         BPL C308D0      ; Exit if not
         LDA $4B         ; Cursor slot
         STA $28         ; Set member slot
         JSR C31E4E      ; Can open menu?
         BCS C31E42      ; Fail if not
         TDC             ; Clear A
         LDA $25         ; Menu to open
         TAX             ; Index it
         LDA.L C31E49-1,X; Init command
C31E3A:  JSR C3B4B8      ; Next: Fade-out
         JMP C30EB2      ; Sound: Click

; Fork: Inaccessible menu
C31E42:  JMP C3B66F      ; Buzzer + Mosaic

; Initialization commands for actor menus
C31E49:  db $19          ; Skills
         db $35          ; Equip
         db $58          ; Relic
         db $0B          ; Status

; 37: Handle multitargeting in main menu
C31EB5:  JSR C33548      ; Redraw time
         LDA $09         ; No-autofire keys
         BIT #$81        ; Using B or right?
         BEQ C31EE5      ; Branch if not
         LDA #$06        ; Next: ST actor
         JSR C32DB_      ; Swap cursors
C31EB_:  LDA $5E         ; Old cursor row
         STA $4E         ; Set as current
         JMP C30EA9      ; Sound: Cursor

; Fork: Handle A
C31EE5:  LDA $08         ; Pushing A?
         BPL C31EB1      ; Exit if not
         LDA #$38        ; Queue: Party gear
         BRA C31E3A      ; Next: Fade-out

; Determine if actor-related menu is accessible
C31E4E:  JSR ld28x2      ; X = Mbr slot
         LDY $6D,X       ; Actor's address
         TYX             ; Index it
         LDA $25         ; Menu to open
         DEC A           ; Skills?
         BEQ C31E89      ; Branch if so
         DEC A           ; Equip?
         BEQ C31E5F      ; Branch if so
         DEC A           ; Relic?
         BEQ C31E74      ; Branch if so
C31EB3:  CLC             ; Mark success
         RTS

; Fork: Equip
C31E5F:  LDA $00,X       ; Actor
         INC A           ; Umaro or guest?
         BRA C31E7_      ; Fail if so

; Fork: Relic
C31E74:  LDA $00,X       ; Actor
C31E7_:  CMP #$0E        ; Guest?
         BCS C31EB1      ; Fail if so

; Fork: Check if dead
C31E9C:  TYX             ; Actor's address
         LDA $14,X       ; Zombie? KO?
         AND #$C2        ; Or Petrify?
         BEQ C31EB3      ; Allow if none
         BRA C31EB1      ; Play buzzer

; Fork: Assign a palette to each submenu in Skills menu
C31E89:  LDA #$24        ; Gray palette
         LDX #$0006      ; 1st: Dance
         PHX             ; Save this value
C34D41:  STA $79,X       ; Disable menu
         DEX             ; Go up one slot
         BPL C34D41      ; Loop till Espers
         JSR C34EDD      ; Actor's address
         LDA #$04        ; Commands: 4
C34D50:  PHA             ; Save counter
         LDX #$0006      ; 1st: Dance
C34D53:  LDA $0016,Y     ; Actor's command
         CMP.L C34D78,X  ; Unlocks submenu?
         BNE C34D60      ; Skip menu if not
         LDA #$20        ; Color: User's
         STA $79,X       ; Enable menu
C34D60:  DEX             ; Go up one slot
         BPL C34D53      ; Loop till Espers
         INY             ; Cmd slot +1
         PLA             ; Loop counter
         DEC A           ; One less cmd
         BNE C34D50      ; Loop till last
         JSR gogoch1     ; Gogo?
         BNE C31E8C      ; Branch if not
         LDA #$24        ; Gray palette
         STA $79         ; Disable "Espers"

; Fork: Check if any skill menu is accessible
C31E8C:  LDA #$24        ; Palette: Gray
         PLX             ; 1st: Dance
C31E90:  CMP $79,X       ; Grayed skill?
         BNE C31E9C      ; Branch if not
         DEX             ; Skill slot -1
         BPL C31E90      ; Loop till Espers
C31EB1:  SEC             ; Mark failure
         RTS

; Battle commands that unlock a skill menu
C34D78:  db $02          ; Magic (unlocks Espers)
         db $02          ; Magic (unlocks Magic)
         db $07          ; SwdTech
         db $0A          ; Blitz
         db $0C          ; Lore
         db $10          ; Rage
         db $13          ; Dance

; 08: Handle Item menu choice 1
C31EF7:  JSR itm_shr     ; Handle D-Pad, etc.
         BCS C31EB1      ; Exit if L or R

; Fork: Handle B
C31F0C:  LDA $09         ; Pushing B?
         BPL C31F3B      ; Branch if not
         LDY $4F         ; Cursor position
         STY $022F       ; To cursor memory
         LDA $4A         ; Scroll position
         STA $0231       ; To cursor memory
C31F0_:  JSR C3B886      ; Load navig data
         JSR C30EA9      ; Sound: Cursor
         JSR b6_1D4E     ; Cursor = Memory?
         BPL C31F2E      ; Branch if not
         LDY $0234       ; Former position
         STY $4D         ; Set as current
C31F2E:  LDA #$17        ; C3/25F4
         STA $26         ; Next: Option list
         JSR C37D86      ; Relocate cursor
         LDY #C38E4A     ; Text pointer
         JSR C302F9      ; Blank item count
         JMP C30F39      ; Queue its upload

; Fork: Handle A
C31F3B:  LDA $08         ; Pushing A?
         BPL C31F55      ; Exit if not
         LDA $4B         ; List slot
         STA $28         ; Set choice 1's
         JMP C3716_      ; Blinker; mode +1

; Handle L, R, and D-Pad for inventory-style menu
itm_shr: TDC             ; List type: Stock
         JSR C31F64      ; Handle L and R
         PHP             ; Save Carry
         BCS .fork       ; Branch if pushed
         JSR C37D22      ; Handle D-Pad
         JSR C382F1      ; Load description
.fork    PLP             ; Restore Carry
dsclist: JSR C3B4E_      ; Description: On
         JMP C30EFD      ; Queue list upload

; Hi-fork: Pressed L on first page
C31F50:  TDC             ; Clear A
         STA $4E         ; Cursor row: 1
C31F52:  STA $50         ; List row: 1
         SEC             ; Mark L/R usage
C31F55:  RTS

; Hi-fork: Pressed R on last page
C31F56:  LDA $54         ; Bottom menu row
         DEC A           ; Adjust value
         STA $4E         ; Set cursor row
         CLC             ; Prepare ADC
         ADC $4A         ; Add scroll pos
         BRA C31F52      ; Set list row

; Hi-fork: Do nothing
C31F62:  CLC             ; Mark no L/R use
         RTS

; Handle L and R for scrollable list
C31F64:  STA $2A         ; Save list type
         LDA $20         ; Scrolling now?
         BNE C31F62      ; Exit if so
         LDA $0A         ; Semi-auto keys
         BIT #$10        ; Pushing R?
         BEQ C31F93      ; Branch if not
         LDA $5C         ; Scroll limit
         SEC             ; Prepare SBC
         SBC $4A         ; Get rows left
         BEQ C31F56      ; Branch if none
         CMP $5A         ; Full page below?
         BCC C31F81      ; Branch if not
         LDA $5A         ; Rows per page
C31F81:  BEQ C31FB7      ; If shift qty = 0
         INC $4A         ; Scroll pos +1
         INC $50         ; List row +1
         DEC A           ; Shift qty -1
         BRA C31F81      ; Loop

; Fork: Handle L
C31F93:  BIT #$20        ; Pushing L?
         BEQ C31F62      ; Exit if not
         LDA $4A         ; On first page?
         BEQ C31F50      ; Branch if so
         CMP $5A         ; Full page above?
         BCC C31FA7      ; Branch if not
         LDA $5A         ; Rows per page
C31FA7:  DEC $4A         ; Scroll pos -1
         DEC $50         ; List row -1
         DEC A           ; Shift qty -1
         BNE C31FA7      ; Loop if > 0

; Fork: Redraw list, play sound effect
C31FB7:  JSR C383F7      ; Define $E5, $E6
         TDC             ; Clear A
         LDA $2A         ; List type
         ASL A           ; Double it
         TAX             ; Index it
         JSR (C31FC4,X)  ; Redraw list
         SEC             ; Mark L/R usage
         JMP C30EA3      ; Sound: Cursor

; Jump table for the above
C31FC4:  dw C37F88       ; Inventory
         dw C34F87       ; Magic
         dw C3523C       ; Lore
         dw C353A7       ; Rage
         dw C3546C       ; Esper
         dw C39CAC       ; Gear

; 0A: Sustain Skills menu
C31FF4:  JSR C3858C      ; Stop desc/bar
         JSR C34B56      ; Handle D-Pad
         LDA $09         ; Pushing B?
         BPL C3200F      ; Branch if not
         JMP C3224D      ; SFX + Main menu

; Fork: Handle A
C3200F:  LDA $08         ; Pushing A?
         BPL C3201C      ; Branch if not
         TDC             ; Clear A
         LDA $4E         ; Cursor row
         STA $5E         ; Set return loc
         TAX             ; Index submenu
         LDA $79,X       ; Name's color
         CMP #$20        ; User's color?
         BNE C3209E      ; Fail if not
         JSR C30EB2      ; Sound: Click
         STZ $4A         ; List scroll: 0
         INC $3A         ; BG2 X-Pos: 256
         INC $3E         ; BG3 X-Pos: 256
         JSR C383F7      ; Define $E5, $E6
         TXA             ; Chosen menu
         ASL A           ; Double it
         TAX             ; Index it
         JMP (C320A5,X)  ; Invoke submenu

; Jump table for the above
C320A5:  dw C320B3       ; Espers
         dw C3211C       ; Magic
         dw C320EE       ; SwdTech
         dw C32105       ; Blitz
         dw C3216E       ; Lore
         dw C321A6       ; Rage
         dw C321DE       ; Dance

; Fork: Play buzzer
C3209E:  JMP C3B66F      ; Buzzer + Mosaic

; Fork: Handle L and R, prepare for menu reset
C3201C:  LDA #$19        ; C3/1B5B
C3201E:  STA $E0         ; Set init command

; Handle member switching with L or R (excludes Status menu)
C32022:  LDA $B5         ; Mosaic settings
         AND #$F0        ; Mosaic enabled?
         BNE lr_exit     ; Exit if so
         JSR swapdir     ; Pushing L or R?
         BEQ lr_exit     ; Exit if not
         LDA $25         ; Submenu
         CMP #$03        ; Relic?
         BNE C32070      ; Branch if not
         JSR C39EEB      ; Handle Reequip
         BCS C32086      ; If triggered

; Fork: Switch to next or previous member
C32070:  JSR swapchr     ; Select member
         JSR C31E4E      ; Can access menu?
         BCS C32070      ; Skip if not
         LDA $E0         ; Init command
         STA $26         ; Set menu mode
C32086:  JMP C30EA3      ; Sound: Cursor

; Build and draw SwdTech, Blitz, or Dance list
draw_ab: PHX             ; Jump table index
         JSR C353C_      ; Build skill list
         JSR C38085      ; Color: User's
         INC $E6         ; BG1 write row +1
         STZ $E5         ; Skill slot: 1
         PLX             ; Jump table index
         LDA #$04        ; Rows left: 4
.loop    PHA             ; Save counter
         PHX             ; Save table index
         JSR (ab_jmp,X)  ; Draw 2 skills
         JSR e6add4      ; BG1 WR row +4
         PLX             ; Jump table index
         PLA             ; Rows left
         DEC A           ; One less left
         BNE .loop       ; Loop till last
lr_exit: RTS

; Jump table for the above
ab_jmp:  dw C35311       ; SwdTech
         dw C3560A       ; Blitz
         dw C357AA       ; Dance

; Initialize SwdTech menu
C320EE:  LDY #$1CF7      ; Known techs adr
         LDX $00         ; Jump table index
         JSR draw_ab     ; Draw SwdTech list
         PEA C35CB8      ; Text pointer
         LDA #$3E        ; C3/298E
         BRA C321D_      ; Branch below

; Initialize Blitz menu
C32105:  LDY #$1D28      ; Known Blitzes adr
         LDX #$0002      ; Jump table index
         JSR draw_ab     ; Draw Blitz list
         PEA C35CB0      ; Text pointer
         LDA #$4D        ; C3/2977
         BRA C321D_      ; Branch below

; Initialize Dance menu
C321DE:  LDY #$1D4C      ; Known Dances adr
         LDX #$0004      ; Jump table index
         JSR draw_ab     ; Draw Dance list
         PEA C35C9F      ; Text pointer
         LDA #$1C        ; C3/22AA
C321D_:  STA $26         ; Next: Sustain menu
         LDY #C34BDF     ; C3/4BDF
         JSR C305FE      ; Load navig data
         JSR C34BD7      ; Relocate cursor
         PLY             ; Text pointer
C35781:  JSR draw2c      ; Draw "Dance"
         JSR C361AC      ; Create portrait
         JMP C30F4D      ; Queue BG3 upload

; Initialize Magic menu
C3211C:  LDY #$050D      ; V-Speed: 5.05 px
         JSR C3216_      ; Create scrollbar

; Fork: Load navigation data, position cursor
C32148:  JSR C34B82      ; Load navig data
         JSR b6_1D4E     ; Cursor = Memory?
         BPL C32155      ; Branch if not
         JSR C30E0A      ; Restore cursor
C32155:  JSR C34B8B      ; Relocate cursor

; Fork: Define boundaries for cursor
C32158:  LDA #$13        ; Scroll limit
         JSR C3597Y      ; Cursor slots: 2x8
         LDY #$0100      ; X: 256
         JSR C3690Y      ; Set for BG2, BG3

; Fork: Draw Magic list and blue "MP…" and create portrait
C34D7F:  JSR C3281C      ; Draw spell list
         LDA #$1A        ; Next: Sustain menu
         LDY #C35C87     ; Text pointer
         JSR C321A_      ; Draw "MP…"; face

; Fork: Draw MP cost in upper corner
C351C6:  JSR C38085      ; Color: User's
         LDY #$81BF      ; Tilemap ptr
         JSR C3BAB_      ; Set position
         JSR wm9E8B      ; WRAM: 7E/9E8B
         JSR C3BFC2      ; Spell by cursor
         JSR mptext      ; Define MP cost
         JMP C3554D      ; Draw 2 digits

; Initialize Espers menu
C320B3:  JSR C34B82      ; Load navig data
         JSR C3597X      ; Move cursor, etc.
         JSR C3546C      ; Draw esper list
         LDY #C35CA7     ; Text pointer
         JSR C35781      ; Title + Face
         LDY #C308D1     ; C3/08D1
         PEA $3638       ; Pos: 56, 54
C32EE_:  JSR qanim2      ; Queue blinker
         JMP C3EA0X      ; Set position

; Initialize Lore menu
C3216E:  LDY #C34C03     ; C3/4C03
         JSR C305FE      ; Load navig data
         JSR C34BFD      ; Relocate cursor
         LDA #$10        ; Top row: Dischord
         LDY #$0108      ; Cursor slots: 1x8
         JSR C3597Z      ; Scroll limit = A
         JSR C3523C      ; Draw Lore list
         LDA #$1B        ; Next: Sustain menu
         LDY #C35C91     ; Text pointer
         JSR C321A_      ; Title + Face
         LDY #$0600      ; V-Speed: 6 px
C3216_:  LDA #$68        ; Y: 104
         JMP C3BC0X      ; Create scrollbar

; Initialize Rage menu
C321A6:  LDY #$00CC      ; V-Speed: 0.8 px
         JSR C3216_      ; Create scrollbar
         JSR C34B82      ; Load navig data
         JSR C34C55      ; Relocate cursor
         LDA #$78        ; Scroll limit
         JSR C3597Y      ; Cursor slots: 2x8
         JSR C353A7      ; Draw Rage list
         LDY #C35C98     ; Text pointer
         LDA #$1D        ; C3/28BA
C321A_:  STA $26         ; Next: Sustain menu
         JMP C35781      ; Title + Portrait

; Test member swap with L or R
swapdir: STZ $E3         ; Swap order: R
         LDA $08         ; No-autofire keys
         BIT #$10        ; Pushing R?
         BNE swapdi_     ; Exit if so
         AND #$20        ; Pushing L?
         BEQ swapdi_     ; Exit if not
         LDA #$FE        ; Swap order: L
         STA $E3         ; Memorize it
swapdi_: RTS             ; Keep A as-is

; 0C: Sustain Status menu
C321F5:  JSR C30F39      ; Set to redraw cmds
         JSR swapdir     ; Pushing L or R?
         BEQ C32244      ; Branch if not

; Fork: Switch to next or previous member
C32224:  LDA $28         ; Member slot
         PHA             ; Memorize it
         JSR swapchr     ; Select member
         PLA             ; Former member
         CMP $28         ; Matches new one?
         BEQ C3223E      ; Skip sound if so
         JSR C30EA3      ; Sound: Cursor
C3223E:  JSR C31C5D      ; Init cursor data
         JSR C35D41      ; Draw "MP" and "/"
         JSR C35F8D      ; Draw actor info
         JSR C35807      ; Delete portrait
         JSR C361AC      ; Create new one
         JSR C30EFD      ; Queue BG1a upload
         JSR C30F61      ; Queue BG3a upload
         JSR C311B0      ; Swap portraits
         JSR C31368      ; Trigger NMI
         LDY #$4800      ; VRAM: $4800
         LDX #$8849      ; Src: 7E/8849
         JSR C30F7_      ; Queue BG3c upload
         JMP C31368      ; Upload BG3c

; Fork: Handle B
C32244:  LDA $09         ; Pushing B?
         BPL C32254      ; Branch if not
C3224D:  LDA #$04        ; Queue: Main menu
C3224F:  JSR C3B4B8      ; Next: Fade-out
         JMP C30EA9      ; Sound: Cursor

; Fork: Handle Gogo
C32254:  JSR gogoch1     ; Gogo?
         BNE swapdi_     ; Exit if not
         TYX             ; Gogo's address
         JSR C33705      ; Handle D-Pad
         LDA $08         ; Pushing A?
         BPL swapdi_     ; Exit if not
         JSR C30EB2      ; Sound: Click
         LDA $4B         ; Cursor slot
         STA $5E         ; Set return loc
.loop    INX             ; Actor adr +1
         DEC A           ; Slot -1
         BPL .loop       ; Loop till < 0
         DEX             ; Rectify address
         LDA $16,X       ; Chosen command
         CMP #$12        ; Mimic?
         BEQ swapdi_     ; Exit if so
         JSR C32F06      ; Create blinker
         LDY #C36412     ; C3/6412
         JSR C305FE      ; Load navig data
         JSR ld_9D89     ; Command list size
         STA $54         ; Set number of rows
         JSR C3640C      ; Relocate cursor
         LDY #$1F65      ; Pan + Cmd list
         JMP C3241X      ; Init scroll data

; 0F: Sustain Config menu
C322C5:  JSR C30EE9      ; Queue list upload
         LDA #$04        ; Button: Down
         BIT $0B         ; Pushing it?
         BEQ C322E0      ; Branch if not
         ASL A           ; Point to slot 8
         CMP $4E         ; On "Controller"?
         BNE C322E0      ; Branch if not
         DEC $26         ; Next: Scroll
         BRA C322F0      ; Set scroll timer

; Fork: Handle upward scrolling
C322E0:  LDA $4A         ; On first page?
         BEQ C32303      ; Branch if so
         JSR key_up      ; Pushing up?
         BEQ C322FA      ; Branch if not
         LDA $4E         ; On top row?
         BNE C322FA      ; Branch if not
         INC $26         ; Next: Scroll
C322F0:  LDA #$11        ; Frames: 17
         STA $20         ; Set scroll timer
         JMP C30EA3      ; Sound: Cursor

; Fork: Handle B and D-Pad
C322FA:  JSR C33884      ; Handle P2 D-Pad
         BRA C32306      ; Skip a line
C32303:  JSR C3385E      ; Handle P1 D-Pad
C32306:  LDA $09         ; Pushing B?
         BMI C3224D      ; Branch if so

; Fork: Handle left and right
C32316:  JSR C32D0B      ; X = List slot x2
         LDA $0B         ; Semi-auto keys
         BIT #$03        ; Left or right?
         BEQ C32325      ; Branch if not
         LSR A           ; Carry = Right key
         LDA $4A         ; On first page?
         BEQ C33D40      ; Branch if so
         JSR C30EA3      ; Sound: Cursor
         JMP (C33D55,X)  ; Handle page 2
C33D40:  PHP             ; Save direction
         CPX #$000A      ; On Sound's row?
         BEQ C33D4_      ; Branch if so
         JSR C30EA3      ; Sound: Cursor
C33D4_:  PLP             ; Carry = Right key
         LDA #$80        ; Common option bit
         JMP (C33D43,X)  ; Handle page 1

; Fork: Handle A
C32325:  LDA $08         ; Pushing A?
         BPL C3232E      ; Branch if not
         LDA $4A         ; On first page?
         BNE C32342      ; Branch if not
         LDA $4E         ; Cursor row
         STA $5F         ; Set return row
         CMP #$03        ; On "Cmd.Set"?
         BEQ C32368      ; Branch if so
         CMP #$08        ; On "Controller"?
         BNE swapch_     ; Exit if not

; Fork: Handle click on "Multiple" option
C32379:  LDA $1D54       ; Multiplayer mode?
         BPL swapch_     ; Exit if not
         LDA #$4B        ; Queue joypad menu
C32383:  JMP C31E3A      ; Sound + Fade-out

; Fork: Handle L and R
C3232E:  BIT #$30        ; Pushing L or R?
         BEQ swapch_     ; Exit if not
         JSR C30EA3      ; Sound: Cursor
         LDA $4A         ; On first page?
         BNE C32406      ; Branch if not

; Fork: Switch to Config page 2
C339EA:  INC $4A         ; Page: 2nd
         STZ $38         ; BG1 Y: -5 -> 251
         LDY #C3388D     ; C3/388D
         JSR C305FE      ; Load navig data
         JMP C33887      ; Relocate cursor

; Fork: Switch to Config page 1
C32406:  STZ $4A         ; Page: 1st
         LDY #$FFFB      ; Y: -5
         STY $37         ; Set BG1 Y-Pos
         JSR C33858      ; Load navig data
         JMP C33861      ; Relocate cursor

; Fork: Handle click on "Short" option
C32368:  LDA $1D4D       ; In Short mode?
         BPL swapch_     ; Exit if not
         LDA #$47        ; Queue cmd menu
         BRA C32383      ; Sound + Fade-out

; Fork: Handle skin palette resetting
C32342:  LDA $4B         ; Cursor slot
         CMP #$02        ; In color area?
         BCC swapch_     ; Exit if not
         JSR C30EB2      ; Sound: Click
         JSR skncol      ; Get viewed color
         BEQ C3239B      ; Branch if font
         JSR getskin     ; Get wallpaper
         PHA             ; Memorize it
         JSR C350F5      ; Skin x15
         TXY             ; Index product
         PLA             ; Wallpaper
         XBA             ; Put it in B
         LDA #$20        ; Src pal size
         JSR C350F_      ; X = Skin x32
;        TDC             ; Skins: 1
         JSR C36BC7      ; Reset colors
         JSR C33A87      ; Refresh skin
         JMP C341C3      ; Redraw RGB info

; Fork: Reset font color
C3239B:  LDY #$7FFF      ; Color: White
         JMP C33FF4      ; Redraw RGB, etc.

; Switch to next or previous member
swapchr: TDC             ; Clear A
         LDA $28         ; Member slot
         SEC             ; Offset ADC
         ADC $E3         ; Point to next
         AND #$03        ; Wrap if needed
         STA $28         ; Save changes
         TAX             ; Index it
         LDA $69,X       ; Empty slot?
         BMI swapchr     ; Loop if so
swapch_: RTS

; 50: Handle Order menu choice 1
C3240C:  JSR C33548      ; Redraw time

; Fork: Copy party info to SRAM block
C36989:  TDC             ; Clear A
         LDX #$0003      ; Member slot: 4
C3698C:  LDA $69,X       ; Actor in slot
         BMI C369A2      ; Skip if nobody
         TAY             ; Index it
         LDA $75,X       ; Party info
         AND #$E7        ; Clear roster slot
         STA $E0         ; Memorize info
         TXA             ; Member slot
         ASL A           ; Put in b1-b2
         ASL A           ; Put in b2-b3
         ASL A           ; Put in b3-b4
         ORA $E0         ; Add saved info
         STA $1850,Y     ; Put in SRAM area
C369A2:  DEX             ; Member slot -1
         BPL C3698C      ; Loop till < 0

; Fork: Handle B and right
C32412:  LDA $09         ; No-autofire keys
         BIT #$81        ; Pushing B or right?
         BEQ C32437      ; Branch if not
         JSR C30EA9      ; Sound: Cursor
         JSR C343C_      ; Cursors: Off
         LDY #$0365      ; Pan + Menu list
C3241X:  JSR C363FX      ; Set scroll timer
C3241Y:  LDY #$000C      ; Speed: 12
         STY $9C         ; Set scroll speed
C32436:  RTS

; Fork: Handle A
C32437:  LDA $08         ; Pushing A?
         BPL C32436      ; Exit if not
         LDA $4B         ; Selected slot
         STA $28         ; Set choice 1
         STA $5E         ; Save for cancel
         JSR C3716_      ; Blinker; mode +1

; Fork: Preload navigation data for portrait selection
C32FB1:  LDX #$000C      ; Base index
C32FB3:  LDA.L C315C7,X  ; Navig data
         STA $80,X       ; Save in RAM
         DEX             ; Index -1
         BPL C32FB3      ; Loop till < 0
C32FBF:  RTS

; 51: Handle Order menu choice 2
C32453:  LDA $09         ; Pushing B?
         BPL C3246F      ; Branch if not
         JSR C31EB_      ; SFX + Old cur pos
         BRA C3246_      ; Branch below

; Fork: Change row
C3246F:  LDA $08         ; Pushing A?
         BPL C32FBF      ; Exit if not
         JSR C30EB2      ; Sound: Click
         JSR ld28        ; First selection
         CMP $4B         ; Matches second?
         BNE C32491      ; Swap if not
         LDA $75,X       ; Party info
         EOR #$20        ; Toggle row
         STA $75,X       ; Save changes
         ASL A           ; Put row in b6
         ASL A           ; Put row in b7
         ASL A           ; Put row in C
         LDA $60,X       ; Queue index
         TAX             ; Index it
         LDA #$01        ; Shift: Left
         ROL A           ; Or right if C+
         JSR C382D8      ; Set anim mode
         LDA #$0C        ; Frames: 12
         STA $20         ; Set wait timer
C3246_:  LDX #$5002      ; Next: Wait
         STX $26         ; Queue: Choice 1
         DEC $46         ; Blinker: Off

; Fork: Disable empty slots
C32F98:  JSR C32FB1      ; Load navig data
         TDC             ; Member slot: 1
C32F9E:  TAX             ; Index slot
         LDY $68,X       ; Actor in slot
         BPL C32FAA      ; Skip if any
         ASL A           ; Actor slot x2
         TAX             ; Index it
         STZ $85,X       ; Disable slot
         LSR A           ; Actor slot / 2
C32FAA:  INC A           ; Member slot +1
         CMP #$04        ; Done all 4?
         BNE C32F9E      ; Loop if not
         RTS

; Fork: Switch members
C32491:  LDA #$11        ; Cursor: Freeze
         TRB $46         ; Blinker: Off
;        LDA #$11        ; C3/24A9
         STA $26         ; Next: Sustain swap
         LDA #$0C        ; Cursor: Blank
         TRB $45         ; Unhide chars: N
         LDA #$18        ; Frames: 24
         STA $22         ; Set anim timer

; Fork: Queue swap animation
C33582:  LDY #C335D4     ; C3/35D4
         BCC C3359D      ; If A is above B
         JSR C335AD      ; Queue B's anim
         LDY #C3361F     ; C3/361F
C33598:  JSR qanim3      ; Queue A's anim

; Fork: Set base scanline for slot A
C335B2:  TXY             ; Queue index
         JSR ld28        ; X = Slot A
         BRA C335C6      ; Branch below

; Fork: First choice is above second
C3359D:  JSR C33598      ; Queue A's anim
         LDY #C3361F     ; C3/361F
C335AD:  JSR qanim3      ; Queue B's anim

; Fork: Set base scanline for slot B
C335C1:  TXY             ; Queue index
         JSR C3BFC2      ; X = Slot B
C335C6:  LDA.L C335D0,X  ; Base scanline
         TYX             ; Queue index
         JMP C30940      ; Put in queue

; Base scanlines for member swap animation
C335D0:  db $0D          ; Member slot 1
         db $3D          ; Member slot 2
         db $6D          ; Member slot 3
         db $9D          ; Member slot 4

; 11: Switch members in Order menu
C324A9:  LDA $22         ; Timer expired?
         BEQ C324E0      ; Finalize if so
         CMP #$0C        ; Unhide point?
         BNE C324ED      ; Exit if not
         JSR C324F8      ; Swap battle pads
         JSR C32DD1      ; Swap actor info
         JSR C324EE      ; Clear icons/faces
         JSR C3318_      ; Redraw party
         JSR C30DBA      ; Swap saved cursors
         JSR C30EFD      ; Queue stats upload
         JSR C32F98      ; Lock empty slots
         JSR C311B0      ; Run anim queue
         JSR C31368      ; Refresh screen
         LDA #$08        ; Unhide members: Y
         BRA C324EA      ; Set menu flag

; Fork: Restore control
C324E0:  LDA #$50        ; C3/240C
         STA $26         ; Next: Choice 1
         LDA #$10        ; Cursor: Unfreeze
         TSB $46         ; Set menu flag
C324E8:  LDA #$04        ; Show cursor: Y
C324EA:  TSB $45         ; Set menu flag
C324ED:  RTS

; Delete all portraits and status icons
C324EE:  JSR C32E3C      ; Portraits: Erase
         LDA #$01        ; Icons: Delete
         TRB $47         ; Set menu flag
         JMP C311B0      ; Run anim queue

; Swap assigned joypad for switched members
C324F8:  TDC             ; Clear A
         TAX             ; Member slot: 1
C32502:  STZ $E0,X       ; Assign Pad 1
         LSR $1D4F       ; Actual joypad
         BCC C32507      ; Skip if Pad 1
         INC $E0,X       ; Assign Pad 2
C32507:  INX             ; Member slot +1
         CPX #$0004      ; Done all 4?
         BNE C32502      ; Loop if not
         PHX             ; Save X
         LDA $28         ; Chosen slot A
         TAX             ; Index it
         LDA $4B         ; Chosen slot B
         TAY             ; Index it
         LDA $E0,X       ; Member A's pad
         PHA             ; Memorize it
         LDA $00E0,Y     ; Member B's pad
         STA $E0,X       ; Set as A's
         PLA             ; Member A's pad
         STA $00E0,Y     ; Set as B's
         PLX             ; Member slot: 4
         TDC             ; Pad 1 for all
.loop    ASL A           ; Shift bits
         ORA $DF,X       ; Add 1 if pad 2
         DEX             ; One less member
         BNE .loop       ; Loop till last
         STA $1D4F       ; Update options
C32536:  RTS

; 14: Sustain Save menu
C32540:  JSR C315AA      ; Handle D-Pad
         LDA $4B         ; Cursor slot
         INC A           ; Now 1-3
         STA $66         ; Set save slot
         JSR C31903      ; Refresh actors
         LDA $09         ; Pushing B?
         BPL C3255D      ; Branch if not
         LDY #$0453      ; Next: Fade-out
C3254_:  STY $26         ; Queue: Main menu
         JMP C30EA9      ; Sound: Cursor

; Fork: Handle A
C3255D:  LDA $08         ; Pushing A?
         BPL C32536      ; Exit if not
         JSR C32D0B      ; Index save slot
         LDY $91,X       ; Blank file?
         BNE C32580      ; Branch if not
         LDX #$1353      ; Next: Fade-out
         BRA C325DF      ; SFX + Save game

; Fork: Invoke confirmation menu
C32580:  JSR C318AC      ; Back up SRAM data
         LDA $66         ; File number
         JSR C31566      ; Load save file
         JSR C36915      ; Move party info
         LDX #$1553      ; Next: Fade-out
C3258_:  STX $26         ; Queue: Submenu
         JMP C30EB2      ; Sound: Click

; 16: Sustain save confirmation
C3259D:  JSR C315DA      ; Handle D-Pad
         LDA $09         ; Pushing B?
         BMI C325C2      ; Abort if so
         LDA $08         ; Pushing A?
         BPL C325DE      ; Exit if not
         LDA $4B         ; Chose "No"?
         BNE C325C7      ; Abort if so
         LDX #$1300      ; Fade-out + Exit

; Fork: Record game data
C325DF:  JSR C325C_      ; Restore SRAM, etc.
         LDY $021B       ; Hrs, mins played
         STY $1863       ; To SRAM block
         LDA $021D       ; Seconds played
         STA $1865       ; To SRAM block
         LDX #$0017      ; Data index
C31588:  LDA $1188,X     ; Timer data
         STA $1FA8,X     ; To SRAM area
         DEX             ; Index -1
         BPL C31588      ; Loop till < 0
         JSR C319D1      ; Compute checksum
         STY $1FFE       ; Put in SRAM block
         LDA $66         ; File number
         STA $021F       ; Link to game
         STA $307FF0     ; Set last altered
         JSR srm_xy      ; Define X and Y
C3154D:  LDA $1600,Y     ; Game data
         STA $306000,X   ; Save in SRAM
         DEX             ; SRAM index -1
         DEY             ; RAM index -1
         BPL C3154D      ; Loop till < 0
         JSR srm_mrk     ; Square's marker
.loop    STA $307FF8,X   ; Mark file
         DEX             ; File index -1
         DEX             ; File index -1
         BPL .loop       ; Loop till < 0
         SEP #$20        ; 8-bit A
         JMP C30EB9      ; Sound: Save

; Fork: Pushing B
C325C2:  JSR C30EA9      ; Sound: Cursor
         BRA C325CA      ; Skip a line

; Fork: Chose "No"
C325C7:  JSR C30EB2      ; Sound: Click
C325CA:  LDX #$1300      ; Next: Fade-out
C325C_:  STX $26         ; Queue: Save menu
         JSR C318C1      ; Restore SRAM
         JSR C36915      ; And party info
C325D9:  LDA $66         ; File number
         STA $0224       ; Set last viewed
C325DE:  RTS

; Set to update and upload description
C31B99:  JSR C3A991      ; Queue desc upload
C31BA1:  LDY #C3A80E     ; C3/A80E
         JMP qanim0      ; Queue desc update

; 17: Handle Item menu options
C325F4:  JSR C3858C      ; Stop desc/bar
         JSR C30EFD      ; Queue stock upload
         JSR C3072D      ; Handle D-Pad
         JSR C37D86      ; Relocate cursor
         LDY $4D         ; Cursor position
         STY $0234       ; To cursor memory
         LDA $09         ; Pushing B?
         BPL C32611      ; Branch if not
         JSR C3224D      ; Queue main menu
C32611:  LDA $08         ; Pushing A?
         BPL C325DE      ; Exit if not
         JSR C30EB2      ; Sound: Click
         LDA $4B         ; On "Use"?
         BEQ C3262C      ; Branch if so
         DEC A           ; On "ARRANGE"?
         BEQ C3267C      ; Branch if so

; Fork: Initialize rare item menu
C3268E:  LDY #C37D53     ; C3/7D53
         JSR C305FE      ; Load navig data
         JSR b6_1D4E     ; Cursor = Memory?
         BPL C3269D      ; Branch if not
         LDY $0232       ; Former position
         STY $4D         ; Set as current
C3269D:  JSR C37D4D      ; Relocate cursor
         JSR C3838B      ; Draw items, count
         JSR C31B99      ; Queue desc anim
         JSR C38339      ; Load description
         LDA #$18        ; C3/2741
C3269_:  STA $26         ; Next: Sustain menu
         JSR C30F39      ; Queue count upload
         JMP C31368      ; Refresh screen

; Fork: Handle "USE" selection
C3262C:  JSR C37D1C      ; Load navig data
         JSR b6_1D4E     ; Cursor = Memory?
         BPL C3263B      ; Branch if not
         JSR C30DAC      ; Restore cursor
         BRA C3264B      ; Skip cursor reset
C3263B:  LDA $0231       ; Old scroll pos
         STA $4A         ; Set as current
         STA $50         ; Set list V-Slot
C3264B:  JSR C37D25      ; Relocate cursor
         JSR C37E0D      ; Draw inventory
         JSR C382F1      ; Desc; item count
         LDA #$08        ; Next: Choice 1
         JSR C3269_      ; Upload BG3, etc.
         JSR C3BC02      ; Create scrollbar
         BRA C31BA1      ; Queue desc update

; Fork: Handle "ARRANGE" selection
C3267C:  LDX #$00FF      ; Item slot: 256
C326BA:  LDA $1869,X     ; Item in slot
         JSR C3AA5E      ; Put in buffer
         LDA #$FF        ; Empty item
         STA $1869,X     ; Put in slot
         LDA $1969,X     ; Item quantity
         STA $7EAB8D,X   ; Put in buffer
         STZ $1969,X     ; Clear original
         DEX             ; Item slot -1
         BPL C326BA      ; Loop till last
C326E0:  INX             ; Icon index: 0
         TXY             ; Item slot: 1
C326E3:  LDA.L C326F5,X  ; Item icon
         PHX             ; Save index

; Fork: Move all items with given icon back to inventory
C32706:  STA $E0         ; Set class
         TDC             ; Clear A
         TAX             ; Buffer slot: 1
C32708:  LDA $7EAA8D,X   ; Item in slot
         CMP #$FF        ; None?
         BEQ C3273A      ; Skip if so
         XBA             ; Put it in B
         LDA.B #!itm_ns  ; Name length
         PHX             ; Save buffer slot
         JSR C350F_      ; X = Item x Length
         LDA.L !itm_np,X ; Item icon
         PLX             ; Buffer slot
         CMP $E0         ; Sought icon?
         BNE C3273A      ; Skip if not
         LDA $7EAA8D,X   ; Item in slot
         STA $1869,Y     ; Put in stock
         LDA $7EAB8D,X   ; Item quantity
         STA $1969,Y     ; Put in stock
         INY             ; Stock slot +1
C3273A:  INX             ; Buffer slot +1
         CPX #$0100      ; Done all 256?
         BNE C32708      ; Loop if not

; Fork: Handle next item class
         PLX             ; Icon index
         INX             ; Index +1
         CPX #$0011      ; Done 17 icons?
         BNE C326E3      ; Loop if not
         JSR C37E0D      ; Redraw inventory
         JSR C3B886      ; Load navig data
         JMP C31F2E      ; Move cursor, etc.

; 18: Sustain rare item menu
C32741:  JSR dsclist     ; Queue list; desc on
         JSR C37D4A      ; Handle D-Pad
         JSR C38339      ; Load description
         LDA $09         ; Pushing B?
         BPL C37FA0      ; Exit if not
         LDY $4D         ; Cursor position
         STY $0232       ; To cursor memory
         JMP C31F0_      ; Return to options

; 09: Handle Item menu choice 2
C32779:  JSR C30EFD      ; Queue list upload
         JSR C37D22      ; Handle D-Pad
         JSR C382F1      ; Load description
         LDA $09         ; Pushing B?
         BPL C32794      ; Branch if not
         DEC $26         ; Next: Choice 1
         DEC $46         ; Blinker: Off
         JMP C30EA9      ; Sound: Cursor

; Fork: Handle A
C32794:  LDA $08         ; Pushing A?
         BPL C37FA0      ; Exit if not
         JSR C30EB2      ; Sound: Click
         DEC $26         ; Next: Choice 1
         DEC $46         ; Blinker: Off
         LDA $28         ; First selection
         CMP $4B         ; Matches second?
         BEQ C38497      ; Branch if not

; Fork: Switch items
C327AA:  JSR C38590      ; Reset/Stop desc
         JSR C38C2B      ; Item A
         PHA             ; Memorize it
         JSR C3BFCB      ; Item B
         STA $1869,X     ; Replace A
         PLA             ; Item A
         STA $1869,Y     ; Replace B
         LDA $1969,X     ; Quantity A
         PHA             ; Memorize it
         LDA $1969,Y     ; Quantity B
         STA $1969,X     ; Replace A
         PLA             ; Quantity A
         STA $1969,Y     ; Replace B

; Fork: Blank and then draw inventory
C37E0D:  JSR C36A15      ; Clear BG1 map A
C37F88:  JSR C383F7      ; Define $E5, $E6
         LDA #$0A        ; Rows left: 10
C37F8E:  PHA             ; Save counter
         JSR C37FA1      ; Draw item info
         INC $E5         ; Item slot +1
         JSR e6add2      ; BG1 WR row +2
         PLA             ; Rows left
         DEC A           ; One less left
         BNE C37F8E      ; Loop till last
C37FA0:  RTS

; Fork: Handle double-clicked item
C38497:  JSR C3BFCB      ; Selected item
         BEQ C37FA0      ; Fail if nothing
         STA $E6         ; Memorize item
         JSR itmtype     ; Item class
         DEC A           ; Tool?
         BMI C37FA0      ; Exit if so
         CMP #$05        ; Consumable?
         BNE C38515      ; Branch if not
         LDA $D85000,X   ; Properties
         ASL A           ; Usable in field?
         BPL C37FA0      ; Fail if not
         LDA $E6         ; Selected item
         CMP #$F7        ; Tent?
         BEQ C384F8      ; Branch if so
         CMP #$FD        ; Warp Stone?
         BEQ C384EB      ; Branch if so
         CMP #$F6        ; Sleeping Bag?
         BNE C384D3      ; Branch if not
         LDA $0201       ; Can save here?
         BPL C37FA0      ; Fail if not
C384D3:  JSR sv_cur      ; Save cursor pos
         LDA #$2F        ; Queue: Usage menu
         JMP C3B4B8      ; Next: Fade-out

; Fork: Warp Stone
C384EB:  LDA $0201       ; Area permissions
         AND #$02        ; Can warp here?
         BEQ C37FA0      ; Fail if not
         INC A           ; Event: Warp
         BRA C38501      ; Destroy item

; Fork: Tent
C384F8:  LDA $0201       ; Can save here?
         BPL C37FA0      ; Fail if not
         LDA #$02        ; Event: Tent
C38501:  STA $0205       ; Set aftereffect
         LDA $E6         ; Item
         JSR C39D97      ; Destroy it
         JMP C3B4B6      ; Next: Fade-out

; Fork: Compile gear's compatible actors
C38515:  JSR wm9D89      ; WRAM: 7E/9D89
         JSR C3BCF_      ; Compatibility
         REP #$20        ; 16-bit A
         LDX $00         ; Current: Terra
C38529:  LSR A           ; Actor can use?
         BCC C38537      ; Skip if not
         PHA             ; Save compat.
         SEP #$20        ; 8-bit A
         TXA             ; Actor number
         STA $2180       ; Add to list
         REP #$20        ; 16-bit A
         PLA             ; Compatibility
C38537:  INX             ; Actor number +1
         CPX #$000E      ; Done all 14?
         BNE C38529      ; Loop if not
         JSR C3C13B      ; End list

; Fork: Initialize gear data menu
C3854A:  LDA #$E0        ; BG1 H-Shift: 224
         STA $7E9A10     ; Hide item list
         DEC $26         ; Next: Sustain menu
         LDA #$04        ; Cursor: Blank
         TRB $45         ; Set menu flag
         JSR C3858C      ; Stop desc/bar
         JSR C38595      ; Create arrow
         LDY #$7D8D      ; Tilemap ptr
         JSR C3BAB_      ; Set text pos
         JSR C3BFCB      ; Y = Item slot
         JSR C380B9      ; Load "<item>:"
         JSR C385AD      ; Add "can be..."
         JSR txt2C       ; Color: Blue
         JSR C38847      ; Draw string
         JSR C38085      ; Color: User's
         JSR C385D5      ; Draw actor list
         JSR C38671      ; Draw other info
         JSR C30F25      ; Queue text upload
         JMP C30F89      ; Dequeue desc DMA

; 1A: Sustain Magic menu
C327E2:  JSR dsclist     ; Queue list; desc on
         JSR C30F75      ; Queue cost upload
         LDA $021E       ; Frame counter
         ROR A           ; Even value?
         BCS C327FB      ; Preserve queue if so
         JSR C3A991      ; Queue desc upload
C327FB:  LDA #$01        ; List type: Magic
         JSR C31F64      ; Handle L and R
         BCS C34F9D      ; Exit if pushed
         JSR C34B88      ; Handle D-Pad
         JSR C35BE3      ; Load desc pointers
         JSR C3572A      ; Load description
         JSR C351C6      ; Draw MP cost

; Fork: Handle Y
C32809:  LDA $09         ; No-autofire keys
         ASL A           ; Pushing Y?
         BPL C32822      ; Branch if not
         JSR C30EB2      ; Sound: Click
         LDA $9E         ; MP display switch
         EOR #$FF        ; Toggle it
         STA $9E         ; Save changes
         JSR C38590      ; Reset/Stop desc
C3281C:  JSR C34F1C      ; Build spell list

; Fork: Draw Magic list
C34F87:  JSR C383F7      ; Define $E5, $E6
         LDA #$08        ; Rows left: 8
C34F8D:  PHA             ; Save counter
         JSR C34F9E      ; Draw 2 spells
         JSR e6add2      ; BG1 WR row +2
         PLA             ; Rows left
         DEC A           ; One less left
         BNE C34F8D      ; Loop till last
C34F9D:  RTS

; Fork: Handle A
C32822:  LDA $08         ; Pushing A?
         BPL C328A0      ; Branch if not
         JSR C3BFC2      ; X = Spell slot
         LDA $7E9E09,X   ; Spell palette
         CMP #$20        ; User's color?
         BNE C32862      ; Fail if not
         JSR C30EB2      ; Sound: Click
         JSR sv_cur      ; Save cursor pos
         LDA $4B         ; Cursor slot
         STA $99         ; Set spell slot
         JSR C32D13      ; Spell
         CMP #$2A        ; Warp?
         BEQ C32874      ; Branch if so
         LDA #$3A        ; Queue: Usage menu
         JMP C3B4B8      ; Next: Fade-out

; Fork: Unusable spell
C32862:  JMP C3B66F      ; Buzzer + Mosaic

; Fork: Warp
C32874:  LDA $0201       ; Area permissions
         AND #$02        ; Can warp here?
         BEQ C32862      ; Fail if not
         INC A           ; Event: Warp
C3287D:  STA $0205       ; Set aftereffect
         JSR C3B4B6      ; Next: Fade-out

; Fork: Decrease caster's MP
C32CEA:  JSR C3510_      ; Define cost
         LDX $0D,Y       ; Actor's MP
.loop    DEX             ; Actor's MP -1
         DEC A           ; MP cost -1
         BNE .loop       ; Loop till 0
         STX $0D,Y       ; Save new MP
C32D02:  RTS

; 1B: Sustain Lore menu
C3288A:  JSR dsclist     ; Queue list; desc on
         LDA #$02        ; List type: Lores
         JSR C31F64      ; Handle L and R
         BCS C32D02      ; Exit if pushed
         JSR C34BFA      ; Handle D-Pad
         LDY.W #!lor_d_p ; Ptr loc: ED/7A70
         LDX.W #!lor_d_l ; Txt loc: ED/77A0
         LDA.B #!lor_d_h ; Bank: ED
         JSR C3571Y      ; Load description
C328A0:  BRA C328C9      ; Handle B button

; 1C: Sustain Dance menu
C328AA:  JSR C30EFD      ; Queue list upload
         JSR C34BD4      ; Handle D-Pad
         BRA C328C9      ; Handle B button

; 1D: Sustain Rage menu
C328BA:  JSR C30EFD      ; Queue list upload
         LDA #$03        ; List type: Rages
         JSR C31F64      ; Handle L and R
         BCS C32D02      ; Exit if pushed
         JSR C34C52      ; Handle D-Pad
C328C9:  BRA C32984      ; Handle B button

; 32: Handle esper selection
C328D3:  JSR dsclist     ; Queue list; desc on
         LDA #$04        ; List type: Espers
         JSR C31F64      ; Handle L and R
         BCS C32E61      ; Exit if pushed
         JSR C34C1E      ; Handle D-Pad
         JSR C35C09      ; Load desc pointers
         JSR C3572A      ; Load description
         LDA $08         ; Pushing A?
         BPL C3291B      ; Branch if not
         JSR C30EB2      ; Sound: Click
         JSR C3BFC2      ; Selected esper
         STA $99         ; Memorize it
         BMI C32908      ; Unequip if none
         INC $26         ; Next: Data menu

; Fork: Initialize esper data menu
C35897:  JSR sv_cur      ; Save cursor pos
         JSR C3599F      ; Draw esper info
         JSR C30F11      ; Queue its upload
         JSR C31368      ; Upload it now
;        TDC             ; Clear A
         INC A           ; BG1 H-Shift: 256
         STA $7E9A11     ; Hide esper list
         LDA #$07        ; BG1 VRAM row: 7
         STA $49         ; Save for V-Shift
         JSR C34E2D      ; Load V-shift data
         LDA #$C0        ; Scrollbar: Off
         TRB $46         ; Set anim index
         JSR C3597D      ; Load navig data
         JMP C35986      ; Relocate cursor

; Fork: Unequip esper
C32908:  JSR C32929      ; Clear esper held
         JSR C3546C      ; Redraw esper list
         JSR C30F11      ; Queue stats upload
         JSR C31368      ; Refresh held esper
         JMP C30EFD      ; Queue list upload

; Fork: Handle B
C3291B:  LDA $09         ; Pushing B?
         BPL C32E61      ; Exit if not
         JSR C32DCC      ; Blinker: Off
         BRA C329A5      ; Leave submenu

; Set to delete all portraits
C32E3C:  LDY #$0003      ; Face slot: 4
C32E3_:  TDC             ; Clear A
.loop    LDA $0060,Y     ; Queue index
         TAX             ; Index it
         LDA #$FF        ; Null value
         STA $7E35C9,X   ; Disable sprite
         DEY             ; Face slot -1
         BPL .loop       ; Loop till < 0
C32E61:  RTS

; 39: Sustain party gear overview
C32966:  LDA $09         ; Pushing B?
         BPL C32E61      ; Exit if not
         JMP C3224D      ; SFX + Main menu

; 4D: Sustain Blitz menu
C32977:  JSR C34BD_      ; Handle D-Pad, etc.
         JSR C35715      ; Load description
C32984:  BRA C3299B      ; Handle B button

; 3E: Sustain SwdTech menu
C3298E:  JSR C34BD_      ; Handle D-Pad, etc.
         LDY.W #!swt_d_p ; Ptr loc: CF/FFAE
         LDX.W #!swt_d_l ; Txt loc: CF/FD00
         LDA.B #!swt_d_h ; Bank: CF
         JSR C3571Y      ; Load description
C3299B:  LDA $09         ; Pushing B?
         BPL C32A20      ; Exit if not

; Fork: Return to Skills menu
C329A5:  LDY $00         ; X: 0
         JSR C3690Y      ; Set for BG2, BG3
         LDA #$0A        ; C3/1FF4
         STA $26         ; Next: Skills menu
         JSR C34D27      ; Erase spell list
         JSR C34B50      ; Load navig data
         JSR C31EB_      ; SFX + Old cur pos
         JSR C34B59      ; Relocate cursor
C35807:  LDY $00         ; Portrait slot: 1
         BRA C32E3_      ; Dequeue portrait

; 21: Sustain Load menu
C329C2:  JSR C315BE      ; Handle D-Pad
         LDA $4B         ; Cursor slot
         STA $66         ; Set save slot
         JSR C31903      ; Refresh actors
         LDA $08         ; Pushing A?
         BPL C32A20      ; Exit if not
         JSR C32D0B      ; X = Cursor slot x2
         TXA             ; On new game?
         BEQ C32A07      ; Branch if so
         LDY $8F,X       ; Blank file?
         BEQ C32A00      ; Fail if so
         JSR C318AC      ; Back up SRAM data
         JSR C325D9      ; File number, etc.
         JSR C31566      ; Load save file
         JSR C36915      ; Move party info
         JSR C31595      ; Move timer data
         LDX #$2253      ; Queue confirm menu
         JMP C3258_      ; Sound + Fade-out

; Fork: Blank file
C32A00:  JMP C3B66F      ; Buzzer + Mosaic

; Fork: New game
C32A07:  JSR C30EB2      ; Sound: Click
C32A0A:  TDC             ; Clear A
         TAY             ; Clear Y
         STY $021B       ; Zero hours, mins
         STY $021D       ; Zero secs, frames
         INC A           ; Save file: 1st
         STA $0224       ; Set last viewed
         STZ $021F       ; Game's file: None
         STZ $0205       ; Set new game mode
         LDX #$FF53      ; Next: Fade-out
         STX $26         ; Queue: Menu exit
C32A20:  RTS

; 23: Sustain load confirmation
C32A2A:  JSR C315DA      ; Handle D-Pad
         LDA $09         ; Pushing B?
         BMI C32A58      ; Abort if so
         LDA $08         ; Pushing A?
         BPL C32A20      ; Exit if not
         JSR C30EB2      ; Sound: Click
         LDA $4B         ; Chose "No"?
         BNE C32A5B      ; Abort if so
         LDY $1863       ; File's hrs, mins
         STY $021B       ; Set as current
         LDA $1865       ; File's seconds
         STA $021D       ; Set as current
         LDA $66         ; Loaded file
         STA $021F       ; Link to game
         JMP C3B4B6      ; Next: Fade-out

; Fork: Chose "No"
C32A58:  JSR C30EA9      ; Sound: Cursor
C32A5B:  JSR C318C1      ; Restore SRAM
         LDA #$20        ; Queue: Load menu
         JMP C3B4B8      ; Next: Fade-out

; Initialize variables for spell or item usage menu
C32A76:  JSR C3352F      ; Reset/Stop stuff
         LDA #$43        ; Ignore back row
         TSB $45         ; And cursor memory
         JSR dma2on      ; Queue Win1 HDMA
         JSR C30F89      ; Halt VRAM DMA B
         LDA #$C0        ; Channels: 6, 7
         TRB $43         ; Halt HDMA 6+7
         JSR C36904      ; Reset BGs' X/Y
         LDY #$FFC0      ; X: -64 = 448
         STY $35         ; Set BG1 X-Pos
         INC $37         ; BG1 Y-Pos = 1
         INC $37         ; BG1 Y-Pos = 2
         JSR C36A19      ; Clear BG1 map B
         JSR C36A28      ; Clear BG2 map A
         JSR C31BA8      ; Load portraits
         JSR C3318A      ; Draw party info
         LDX #C3583F     ; C3/583F
         JSR C3034Y      ; Draw windows
C32A7X:  JSR C307B0      ; Queue cursor OAM
C32A7Y:  JSR C32F98      ; Preload nav data
         LDY #C32FC0     ; C3/2FC0
         JMP qanim0      ; Queue D-Pad fn

; 3B: Handle single-target spell
C32AAE:  LDA $09         ; No-autofire keys
         BIT #$03        ; Left or right?
         BNE C32AC6      ; Branch if so
         LDA $08         ; No-autofire keys
         BIT #$30        ; Pushing L or R?
         BEQ C32AF3      ; Branch if not
C32AC6:  JSR C30EA3      ; Sound: Cursor
         JSR C32D13      ; Spell number
         JSR C350F5      ; Compute index
         LDA $C46AC0,X   ; Spell properties
         AND #$20        ; Can multi-target?
         BEQ C32AF3      ; Branch if not
         LDA $4E         ; Cursor row
         STA $5F         ; Save for revert
         LDA #$3D        ; Next: Target all
         JMP C31E0_      ; Create blinkers

; Fork: Handle A
C32AF3:  LDA $08         ; Pushing A?
         BPL C32AFC      ; Branch if not
         TDC             ; Halve HP gain: N
         JSR C32CC_      ; Get LV, Mag.Pwr
         JSR C32D0B      ; Target's address
         JSR C32C14      ; Valid target?
         BCC C32B32      ; Fail if not
         PHY             ; Memorize address
         JSR C32CEA      ; Lower caster's MP
         JSR C30EE5      ; Play heal sound
         PLY             ; Target's address
         JSR C32B39      ; Adjust HP, status
         JMP C32BDE      ; Redo GFX, test MP

; Fork: Invalid target
C32B32:  JMP C3B66F      ; Buzzer + Mosaic

; Fork: Handle B
C32AFC:  LDA $09         ; Pushing B?
         BPL ld_ail_     ; Exit if not
C32AF_:  LDA #$3C        ; Queue: Magic menu
         JMP C3224F      ; Sound + Fade-out

; Adjust spell target's HP and status
C32B39:  JSR ld_ail      ; Load status
         LDX $0B,Y       ; Max HP
         STX $11B2       ; Set target's
         JSR C32D13      ; Spell number
         LDX $00         ; Mode: Magic
         JSL $C20009     ; Get gain/status
         LDA $9C         ; One target?
         REP #$21        ; 16-bit A; C-
         BEQ C32B5C      ; Branch if so
         LSR $11B0       ; Halve HP gain
C32B5C:  LDA $11B0       ; HP gain
         JSR add_hp      ; Add to actor HP
         JSR st_ail      ; Set new status
         LDA $9C         ; One target?
         BNE ld_ail_     ; Exit if not
         JSR C3BFC2      ; X = Member slot
         JSR C32CE3      ; Get gear data
         LDA $11D2       ; Immunities
         JSR C391EC      ; Deny ailments
         LDA $11D4       ; Gear auto status
         LSR A           ; Put Float in C
         TDC             ; Clear A
         ROR A           ; Put Float in b7
         ORA $15,X       ; Add actor status
         BRA st_ail_     ; Set actor status

; Load status effects for updating
ld_ail:  LDX $14,Y       ; Status LB
         STX $F8         ; Set target's
         LDX $15,Y       ; Status HB
         STX $FB         ; Set target's
ld_ail_: RTS

; Apply updated status effects
st_ail:  LDX $FC         ; Status LB
         STX $14,Y       ; Set actor's
         LDA $FF         ; Status HB
st_ail_: STA $0015,Y     ; Set actor's
         RTS

; Redraw party after spell or item usage
C32C01:  LDA $4B         ; Cursor slot, 0 if MT
         STA $9C         ; Set portrait slot
         JSR C324EE      ; Clear icons/portraits
         JSR C3318_      ; Redo stats/icons/faces
         JSR C36C09      ; Load portrait colors

; Fork: Set to upload healed member's portrait
C36EBF:  TDC             ; Clear A
         LDA $9C         ; Member slot
         ASL A           ; Double it
         TAX             ; Index it
C36EBW:  LDY $6D,X       ; Actor's address
         REP #$20        ; 16-bit A
C36EBX:  LDA.L C36F51,X  ; VRAM pointer
         STA $1B         ; Set destination
         TYX             ; Actor's address
         JSR gfxset2     ; Portrait number
         BEQ C36EEF      ; Branch if Imp's
C36EE5:  LDA $00,X       ; Actor
         CMP #$01        ; Locke?
         BEQ C36EEF      ; Branch if so
C36EBZ:  TDC             ; Clear A
         LDA $01,X       ; Graphics set
C36EEF:  REP #$20        ; 16-bit A
         ASL A           ; Double it
         TAX             ; Index it
         LDA.L C36F1B,X  ; GFX pointer
         ADC #$1D00      ; Add base adr
         STA $1D         ; Set src LBs
         SEP #$20        ; 8-bit A
         LDA #$ED        ; Bank: ED
         STA $1F         ; Set src HB
         LDY #$0320      ; Bytes: 800
         STY $19         ; Set data size
         RTS

; Establish whether spell target is valid
C32C14:  LDA $0014,Y     ; Status
         ASL A           ; Wound?
         JSR C32D13      ; Spell number
         BCS C32C40      ; Branch if Wound
         TYX             ; Actor's address
         CMP #$2C        ; Dispel?
         BEQ C32C56      ; Branch if so
         BCC C32C2A      ; Branch if lower
         CMP #$30        ; Cure family?
         BCC C32C76      ; Branch if so
C32C2A:  CMP #$32        ; Antdot?
         BEQ C32C6D      ; Branch if so
         CMP #$33        ; Remedy?
         BEQ C32C64      ; Branch if so
         CMP #$23        ; Imp?
         BEQ C32C83      ; Succeed if so
         CMP #$22        ; Float?
         BNE C32C82      ; Fail if not

; Fork: Float
C32C4D:  LDA $15,X       ; Float status?
         BMI C32C82      ; Fail if so
         RTS             ; Spell will work

; Fork: Wound
C32C40:  CMP #$30        ; Life?
         BEQ C32C83      ; Succeed if so
         CMP #$31        ; Life 2?
         BNE C32C82      ; Fail if not
         RTS             ; Spell will work

; Fork: Dispel
C32C56:  LDA $14,X       ; Status LB
         ORA $15,X       ; Add high byte
         AND #$90        ; Clear or Float?
         BRA C32C72      ; Fail if neither

; Fork: Remedy
C32C64:  LDA $14,X       ; Dark? Poison?
         AND #$45        ; Or Petrify?
         BRA C32C72      ; Fail if none

; Fork: Antdot
C32C6D:  LDA $14,X       ; Status
         AND #$04        ; Poison?
C32C72:  BNE C32C83      ; Succeed if not
C32C82:  CLC             ; Make spell fail
C32C83:  RTS

; Fork: Cure spells
C32C76:  LDA $14,X       ; Zombie? KO?
         AND #$C2        ; Or Petrify?
         BNE C32C82      ; Fail if any
         JMP C32B9A      ; Or if full HP

; Load caster's Mag.Pwr and level (for C2/0009)
C32CC_:  STA $9C         ; Set heal mode
C32CCC:  JSR C32CDF      ; Define stats
         LDA $11A0       ; Mag.Pwr
         STA $11AE       ; Memorize it
         JSR C34EDD      ; Actor's address
         LDA $0008,Y     ; Level
         STA $11AF       ; Memorize it
         RTS

; Load member's stats and properties with gear
C32CDF:  JSR ld28        ; X = Mbr slot
C32CE3:  LDA $69,X       ; Actor
C32CE5:  JSL $C20006     ; Load data
         RTS

; Get selected spell
C32D13:  TDC             ; Clear A
         LDA $99         ; Spell slot
         JMP sk_slt5     ; Spell in slot

; 3C: Return to Magic menu
C32D1C:  JSR C3352F      ; Reset/Stop stuff
         JSR C36A08      ; Set Win1 bounds
         LDA #$42        ; Allow back row
         TRB $45         ; And cursor memory
         JSR C34DBC      ; Set to shift text
         JSR C34C80      ; Draw menu, etc.
         JSR C307B0      ; Queue cursor OAM
         JSR C3211C      ; Text, cursor, bar
         JSR oldcur      ; Restore cursor pos
         JSR C34B8B      ; Relocate cursor
         JSR C351C6      ; Draw MP cost
         JSR C31368      ; Refresh screen
;        TDC             ; Clear A
         TAY             ; Clear Y
         STY $35         ; BG1 X-Pos: 0
         JSR C31B99      ; Queue desc anim
         JSR C38590      ; Reset/Stop desc
         JSR C30EFD      ; Queue lists upload
         LDA #$1A        ; Queue Magic menu

; Fork: Set queued menu command, trigger fade-in
C32AA5:  JSR C31C15      ; Set cmd; BRT:1
         JMP C39EAE      ; Next: Fade-in

; 3A: Initialize spell usage menu
C32A65:  JSR C32A76      ; Init variables
         LDX #C3588_     ; Text ptrs loc
         JSR C3905F      ; Draw "MP Needed"
         LDA $4B         ; Cursor slot
         STA $E5         ; Set spell slot
         JSR C34FB5      ; Define source
         LDY #$790D      ; Tilemap ptr
         JSR C3BAB_      ; Set position
         JSR sk_slt4     ; Spell
         PHA             ; Memorize it
         JSR C334FY      ; Draw spell name
         PLA             ; Spell
         JSR C3510D      ; Define MP cost
         LDX #$7A0F      ; Text position
         JSR C304B_      ; Draw 2 digits
         JSR C3319F      ; Upload tilemaps
         LDA #$3B        ; Queue: Sustain menu
         BRA C32AA5      ; Next: Fade-in

; 3D: Handle multitarget spell
C32D78:  LDA $09         ; No-autofire keys
         BIT #$03        ; Left or right?
         BNE C32D90      ; Branch if so
         LDA $08         ; No-autofire keys
         BIT #$30        ; Pushing L or R?
         BEQ C32D9B      ; Branch if not
C32D90:  LDA #$3B        ; Next: One target
         JSR C32DB_      ; Swap cursors
         JMP C30EA3      ; Sound: Cursor

; Fork: Handle B
C32DA4:  LDA $09         ; Pushing B?
         BPL C32DD0      ; Exit if not
         JSR C32AF_      ; Sound + Fade-out
         BRA C32DB7      ; Swap cursors

; Fork: Handle A
C32D9B:  BPL C32DA4      ; If not pushing A
         STZ $AF         ; Valid target: N
         LDA #$01        ; Half gain, etc.
         JSR C32CC_      ; Get LV, Mag.Pwr
         LDA #$03        ; Member slot: 4
C32C90:  PHA             ; Save member
         JSR C32D0E      ; Target's address
         BEQ C32CB8      ; Skip if nobody
         JSR C32C14      ; Valid target?
         BCC C32CB8      ; Skip if not
         JSR C32B39      ; Adjust HP, status
         JSR C32CCC      ; Get LV, Mag.Pwr
         LDA $AF         ; 1st valid target?
         BNE C32CB8      ; Branch if not
         JSR C30EE5      ; Play heal sound
         JSR C32CEA      ; Lower caster's MP
         INC $AF         ; Valid target: Y
C32CB8:  TDC             ; Clear A
         PLA             ; Member slot
         DEC A           ; Member slot -1
         BPL C32C90      ; Loop till last
         LDA $AF         ; Is any valid?
         BNE C32BDE      ; Branch if so
         JSR C3B66F      ; Buzzer + Mosaic

; Fork: Redraw party, exit menu if short on MP
C32BDE:  JSR C32C01      ; Redraw party
         JSR C32CDF      ; Check caster gear
         JSR C3510_      ; Define spell cost
         LDX $0D,Y       ; Caster's MP
         CPX $E0         ; Can recast?
         BCS C32DD0      ; Exit if so
         LDA #$02        ; Main cursor: On
         STA $46         ; Bar/Blinker: Off
         LDA #$3C        ; Queue: Magic menu
         JMP C3B4B8      ; Next: Fade-out

; Revert to a single portrait for targeting
C32DB_:  STA $26         ; Set menu cmd
C32DB7:  JSR C32A7X      ; Set up cursor
         JSR C311B0      ; Run anim queue
         LDA $5F         ; Old cursor row
         STA $4E         ; Set as current
C32DCC:  LDA #$08        ; Blinkers: Off
         TRB $46         ; Set menu flag
C32DD0:  RTS

; Swap actor info for switched members
C32DD1:  JSR ld28        ; X = Slot A
         LDA $4B         ; Chosen slot B
         TAY             ; Index it
         LDA $75,X       ; A's party info
         PHA             ; Memorize it
         LDA $0075,Y     ; B's party info
         STA $75,X       ; Set as A's
         PLA             ; A's party info
         STA $0075,Y     ; Set as B's
         LDA $69,X       ; A's actor ID
         PHA             ; Memorize it
         LDA $0069,Y     ; B's actor ID
         STA $69,X       ; Set as A's
         PLA             ; A's actor ID
         STA $0069,Y     ; Set as B's
         JSR ld28x2      ; X = Slot A x2
         TYA             ; Chosen slot B
         ASL A           ; Double it
         TAY             ; Reindex it
         REP #$20        ; 16-bit A
         LDA $6D,X       ; A's data adr
         PHA             ; Memorize it
         LDA $006D,Y     ; B's data adr
         STA $6D,X       ; Set as A's
         PLA             ; A's data adr
         STA $006D,Y     ; Set as B's
         SEP #$20        ; 8-bit A
         RTS

; Invoke Config menu
C32E7A:  STZ $5F         ; Cursor: Bat.Mode
         LDA #$0D        ; Queue: Config menu
         BRA C32E8_      ; Sound + Fade-out

; Invoke Item menu
C32E86:  LDA #$64        ; Queue Item menu
C32E8_:  JMP C31E3A      ; Sound + Fade-out

; Initialize portrait targeting with main menu command
C32E90:  LDA #$02        ; List cursor: Off
         TRB $46         ; Set menu flag
         JSR C32A7Y      ; Set up cursor
         JSR C32F06      ; Create blinker
         LDA #$06        ; C3/1DE8
         STA $26         ; Next: Targeting
         JMP C30EB2      ; Sound: Click

; Invoke Save menu if allowed
C32EAA:  LDA #$13        ; Menu init cmd
         LDX $0200       ; Can save here?
         BMI C32E8_      ; Invoke menu if so
         JMP C3B66F      ; Buzzer + Mosaic

; Create blinking cursor to the right of main cursor
C32F06:  LDA $56         ; Cursor's Y
         PHA             ; Memorize it
         LDA $55         ; Cursor's X
         INC A           ; +1 px right
         INC A           ; +1 px right
         PHA             ; Memorize it
         LDY #C307FF     ; C3/07FF
         JMP C32EE_      ; Queue OAM fn

; Create blinking cursor to northeast of main cursor
C32F21:  LDY #C307FF     ; C3/07FF
         JSR qanim1      ; Queue OAM fn
         LDA $56         ; Cursor's Y
         SEC             ; Prepare ADC
         SBC #$04        ; +4 px up
         PHA             ; Memorize it
         LDA $55         ; Cursor's X
         ADC #$03        ; +4 px right
         JMP C3EA0Y      ; Set position

; Update cursor position in main menu list
C32F42:  BNE C32F6B      ; If mode 1

; Mode 0: Initialize data
C32F4A:  LDA #$02        ; Show cursor: Y
         TSB $46         ; Set menu flag
         INC $3649,X     ; Mode +1
         JSR C3597D      ; Load navig data
         JSR b6_1D4E     ; Cursor = Memory?
         BPL C32F6B      ; Branch if not
         LDY $022B       ; Former position
         STY $4D         ; Set as current

; Mode 1: Update position
C32F6B:  LDA $46         ; Menu flags
         LSR A           ; Shift bits
         LSR A           ; Delete cursor?
         BCC C32F82      ; Dequeue if so
         JSR C3072D      ; Handle D-Pad
         LDY #C32F8A     ; C3/2F8A
         JSR C30640      ; Set cursor X/Y
         LDY $4D         ; Cursor position
         STY $022B       ; To cursor memory
         SEC             ; Set to requeue
C32F82:  RTS

; Update movable cursor's position for portrait selection
C32FC0:  BNE C33014      ; If mode 1

; Mode 0: Initialize data
C32FC8:  LDA #$14        ; Main cursor: On
         TSB $46         ; Enable D-Pad
         INC $3649,X     ; Mode +1
         JSR C32FF5      ; Load navig data
         LDA $45         ; Menu flags
         ASL A           ; Skip cursor mem?
         BMI C32FE6      ; Branch if so
         JSR b6_1D4E     ; Cursor = Memory?
         BPL C32FE6      ; Branch if not
         LDY $022D       ; Former position
         STY $4D         ; Set as current
C32FE6:  JSR C33008      ; Relocate cursor
         LDA $55         ; On empty slot?
         BNE C32FF3      ; Exit if not
         JSR C32FF5      ; Target leader
         JSR C33008      ; Relocate cursor
C32FF3:  SEC             ; Set to requeue
         RTS

; Mode 1: Update position
C33014:  LDA $45         ; Menu flags
         ASL A           ; Skip cursor mem?
         BMI C3301F      ; Branch if so
         LDY $4D         ; Cursor position
         STY $022D       ; To cursor memory
C3301F:  CLC             ; Preset to dequeue
         LDA $46         ; Menu flags
         BIT #$04        ; Delete cursor?
         BEQ C3303F      ; Dequeue if so
         BIT #$10        ; Swapping chars?
         BEQ C3303E      ; Exit if so
C3302B:  JSR C3072D      ; Handle D-Pad
         JSR C33008      ; Relocate cursor
         LDA $55         ; On empty slot?
         BEQ C3302B      ; Retarget if so
C3303E:  SEC             ; Set to requeue
C3303F:  RTS

; Load custom nav data for portrait selection, target leader
C32FF5:  LDY #$0080      ; 00/0080
         TDC             ; Source HB: 00
         TAX             ; Slot index: 0
         JSR C30600      ; Load navig data
C30620:  LDA $85,X       ; Nobody in slot?
         BNE C3303F      ; Exit if not
         INC $4E         ; Cursor row +1
         INX             ; Slot index +1
         INX             ; Slot index +1
         BRA C30620      ; Try next slot

; Set portrait cursor's slot and base pixel position
C33008:  LDY #$0085      ; 00/0085
         STY $E7         ; Set src LBs
         STZ $E9         ; Zero src HB
         JMP C30643      ; Relocate cursor

; Queue menu fade-out
C3304F:  LDY #C3309F     ; C3/309F
         BRA C3305A      ; Put in queue

; Queue menu fade-in
C33056:  LDY #C330CA     ; C3/30CA
C3305A:  JMP qanim0      ; Put in queue

; Process queued mosaic effect
C33064:  BNE C33079      ; If mode 1

; Mode 0: Initialize mosaic
C3306C:  INC $3649,X     ; Mode +1
         STZ $33CA,X     ; 1st: C3/3097
         LDA #$08        ; Frames: 8
         STA $3349,X     ; Set timer

; Mode 1: Sustain mosaic
C33079:  CLC             ; Preset to dequeue
         LDA $3349,X     ; Timer expired?
         BEQ C332EA      ; Dequeue if so
         LDA $33CA,X     ; Table index
         TAX             ; Index it
         LDA.L C33097,X  ; Parameters
         STA $B5         ; Save in RAM
         TYX             ; Queue index
         INC $33CA,X     ; Table index +1
         JMP C3DF59      ; Timer -1

; Mosaic parameters
C33097:  db $17          ; Intensity 1 for BG1-BG3
C33098:  db $27          ; Intensity 2 for BG1-BG3
C33099:  db $37          ; Intensity 3 for BG1-BG3
C3309A:  db $47          ; Intensity 4 for BG1-BG3
C3309B:  db $37          ; Intensity 3 for BG1-BG3
C3309C:  db $27          ; Intensity 2 for BG1-BG3
C3309D:  db $17          ; Intensity 1 for BG1-BG3
C3309E:  db $07          ; Intensity 0 for BG1-BG3

; Process queued menu fade-out
C3309F:  DEC $44         ; Brightness -1
;        DEC $44         ; Brightness -1
         JMP C3D1FD      ; Handle timer

; Process queued menu fade-in
C330CA:  BNE C330DC      ; If mode 1

; Mode 0: Initialize brightness
C330D2:  INC $3649,X     ; Mode +1
         LDA #$FF        ; Brightness: -1
         STA $44         ; Set for INC x2

; Mode 1: Increase brightness
C330DC:  INC $44         ; Brightness +1
         INC $44         ; Brightness +1
         SEC             ; Preset to requeue
         LDY $20         ; Timer expired?
         BNE C332EA      ; Exit if not
         JMP C3D1F7      ; BRT: 15; dequeue

; Enforce GP cap
C332CE:  LDY #$967F      ; Cap LBs
         CPY $1860       ; C- if < GP LBs
         LDA #$98        ; Cap HB
         SBC $1862       ; Deduct GP w/ C
         BCS C332EA      ; Exit if < 10M
         STY $1860       ; Set gold LBs
         LDA #$98        ; Cap HB
         STA $1862       ; Set gold HB
C332EA:  RTS

; Draw and upload main menu
C330F5:  JSR bg2b_cl     ; Clear BG2 maps
         JSR C36A19      ; Clear BG1 map B
         LDX #C331CF     ; C3/31BF
         JSR C3034Y      ; Draw Order box
         JSR C33170      ; Draw stats, etc.
         LDX #C331BB     ; C3/31BB
         JSR C30341      ; Draw option box
         JSR C3326C      ; Draw time
         LDX #C33729     ; Text ptrs loc
         LDA #$06        ; Strings: 6
         JSR C369BA      ; Draw Item..Config
         LDA $0201       ; Can save here?
         BMI C3321A      ; Branch if so
         JSR txt24       ; Color: Gray
C3321A:  LDY #C337FD     ; Text pointer
         JSR C302F9      ; Draw "Save"
         BRA C3319F      ; Upload tilemaps

; Draw and upload save confirmation menu
C33128:  PEA C33765      ; Text ptrs loc
         LDA #$05        ; Strings: 5
         BRA C3314_      ; Branch below

; Draw and upload load confirmation menu
C3314C:  PEA C3375D      ; Text ptrs loc
         LDA #$04        ; Strings: 4
C3314_:  PHA             ; Save counter
         JSR C36E82      ; Load portraits
         JSR C331A8      ; Upload skin
         JSR C33170      ; Draw stats, etc.
         LDX #C331CB     ; C3/31CB
         JSR C30341      ; Draw Yes/No box
         JSR C33295      ; Draw time
         PLA             ; String count
         PLX             ; Text ptrs loc
         JSR C369BA      ; Draw question

; Upload tilemaps for menu with portraits
C3319F:  JSR C30E52      ; Upload windows
         JSR mvmapx2     ; Upload BG1+3 A+B
         JMP C30E7C      ; Upload BG3 C+D

; Draw elements shared by main menu and save file submenus
C33170:  TDC             ; Gradient set: 0
         JSL $D4CA1D     ; Create gradient
         JSR C33F99      ; Apply user font
         LDX #C331C7     ; C3/31C7
         JSR C3034Y      ; Draw main boxes
         JSR C3318A      ; Draw party info

; Fork: Draw game stats
C33221:  JSR C332CE      ; Cap GP
         JSR C3058X      ; Convert GP to text
         LDX #$7EB7      ; Text position
         JSR C304AC      ; Draw GP total
         LDY #C3380B     ; Text pointer
         JSR C302F9      ; Draw ":"
         LDY $1866       ; Steps LBs
         LDA $1868       ; Steps HB
         JSR C3058_      ; Turn into text
         LDX #$7DF7      ; Text position
         JSR C304AC      ; Draw step count
         LDX #C33723     ; Text ptrs loc
C3322X:  JSR txt2C       ; Color: Blue
         JMP C39055      ; Draw SE titles

; Draw time in main menu
C3326C:  LDA $1188       ; Timer 0 flags
         BIT #$10        ; Override clock?
         BEQ C33289      ; Branch if not
         LDX $1189       ; Timer countdown
         JSR C332B2      ; Convert to secs
         JSR C332B2      ; Convert to mins
         STX $1863       ; Set shown hours
         LDA $4216       ; Timer's seconds
         STA $1864       ; Set shown mins
         BRA C33295      ; Draw time

; Fork: Normal clock
C33289:  LDY $021B       ; Hours, mins
         STY $1863       ; Save in SRAM
         LDA $021D       ; Seconds
         STA $1865       ; Save in SRAM

; Fork: Draw time
C33295:  JSR C38085      ; Color: User's
         LDA $1863       ; Shown hours
         LDX #$7CFB      ; Text position
         JSR C304B_      ; Draw 2 digits
         JSR C304F_      ; Convert minutes
         LDX #$7D01      ; Text position
         JMP C304B6      ; Draw 2 digits

; Divide countdown timer's value by 60
C332B2:  LDA #$3C        ; Divisor: 60
divide:  STX $4204       ; Set dividend
         STA $4206       ; Set divisor
         JSR x16         ; Waste 20 cycles
         LDX $4214       ; Quotient
         RTS

; Draw party info
C3318A:  JSR bg3c_cl     ; Clear BG3 maps
C3318_:  JSR C36A15      ; Clear BG1 map A
C332F1:  LDX #$0003      ; Member slot: 4
.loop1   LDA $69,X       ; Vacant slot?
         BMI .next       ; Skip if so
         PHX             ; Save member slot
         PHX             ; Save it again
         JSR C30A22      ; Create portrait
         REP #$20        ; 16-bit A
         PLA             ; Member slot
         JSR C34352      ; Actor's address
         LDY #$0006      ; Parameters: 6
.loop2   LDA.L ptytbl,X  ; Ptr or position
         PHA             ; Memorize it
         TXA             ; Parameter index
         CLC             ; Prepare ADC
         ADC #$0008      ; Point to next
         TAX             ; Reindex it
         DEY             ; One less left
         BNE .loop2      ; Loop till last
         SEP #$20        ; 8-bit A
         JSR txt24       ; Color: Blue
         PLX             ; Text ptrs loc
         JSR drawx3      ; Draw "LV/HP/MP"
         PLX             ; Icon position
         PLY             ; Class/KO pos
         JSR C3625_      ; Display status
         PLX             ; Text ptrs loc
         JSR C39063      ; Draw both "/"
         PLY             ; Text position
         JSR C334CF      ; Draw actor name
         PLX             ; Coords tbl ptr
         JSR C30C6C      ; Draw LV, HP, MP
         PLX             ; Member slot
.next    DEX             ; Member slot -1
         BPL .loop1      ; Loop till last
         RTS

; Draw actor name
C334CF:  JSR C33519      ; Set pos/WRAM/Y
C334D2:  LDX #$0006      ; Letters: 6
C334D5:  LDA $0002,Y     ; Letter
         STA $2180       ; Add to string
         INY             ; Point to next
         DEX             ; One less left
         BNE C334D5      ; Loop till last
         JMP C33502      ; Draw string

; Prepare name drawing
C3351_:  LDY $F5         ; Tilemap ptr
C33519:  JSR wm9E8B      ; WRAM: 7E/9E8B
         JSR C3BAB_      ; Set position
;        TDC             ; Clear A
         LDY $67         ; Actor's address
         RTS

; Reset OAM and anim queue; disable screen, interrupts, pad, HDMA
C3352X:  JSR C36904      ; Reset BGs' X/Y
C3352Y:  LDA #$02        ; Main cursor: On
         STA $46         ; Bar/Blinker: Off
C3352F:  LDA #$80        ; BRT:0 and OFF
         STA $2100       ; Disable screen
         JSR C31114      ; Reset OAM, queue
         STZ $4200       ; Disable INT, pad
         STZ $420C       ; Disable HDMA
         RTS

; Redraw time in main menu
C33548:  JSR C30F39      ; Queue BG3 upload
         JMP C3326C      ; Draw time

; Queue main screen designation HDMA for main menu
C3354E:  LDX #$00DE      ; Table index
         LDA #$17        ; BG1-3, sprites
C33552:  STA $7E9A09,X   ; Set MS desig.
         DEX             ; Index -1
         BPL C33552      ; Loop till < 0
         LDY #C3357B     ; Src: C3/357B
         LDA #$C3        ; Bank: C3
         STA $4364       ; Set src HB
         LDA #$7E        ; Bank: 7E
         STA $4367       ; Set indir HB
         LDX #$2C40      ; 1Rx1B to $212C
         JMP dma6b       ; Queue HDMA-6

; Main screen designation table for main menu
C3357B:  db $F0,$09,$9A  ; 112 lines, use $9A09
         db $F0,$79,$9A  ; 112 lines, use $9A79
         db $00          ; End

; Erase member slot contents from the top down, and vice versa
C335D4:  BNE C33602      ; If mode 1

; Mode 0: Erase 4 lines
C335E3:  LDA $22         ; Time left
         CMP #$0C        ; 12 frames?
         BEQ C335FA      ; Unmask if so
         LDA $33CA,Y     ; Scanline
         TAX             ; Index it
         LDA #$06        ; On MS: BG2+BG3
         SEC             ; Mode: Down
         JSR C3368_      ; Hide 4 lines
         BRA C3367_      ; Save scanline

; Fork: Start unmasking
C335FA:  INC $3649,X     ; Mode +1
         DEC $33CA,X     ; Scanline -1

; Mode 1: Print 4 lines
C33602:  LDA $45         ; Menu flags
         BIT #$08        ; Got go-signal?
         BEQ C33673      ; Exit if not
         CLC             ; Preset to dequeue
         LDA $22         ; Timer expired?
         BEQ C33674      ; Dequeue if so
         LDA $33CA,Y     ; Scanline
         TAX             ; Index it
         LDA #$17        ; On MS: BGs+OBJ
         JSR C3368D      ; Unhide 4 lines
         BRA C3367_      ; Save scanline

; Erase member slot contents from the bottom up, and vice versa
C3361F:  CMP #$02        ; Mode 1?
         BEQ C33637      ; Branch if so
         BCS C33658      ; If mode 2

; Mode 0: Start from bottom
C33629:  INC $3649,X     ; Mode +1
         LDA $33CA,X     ; Top scanline
         ADC #$2F        ; Go 47 px down
         STA $33CA,X     ; Save changes

; Mode 1: Erase 4 lines
C33637:  LDA $22         ; Time left
         CMP #$0C        ; 12 frames?
         BEQ C33650      ; Unmask if so
         LDA $33CA,Y     ; Scanline
         TAX             ; Index it
         LDA #$06        ; On MS: BG2+BG3
         JSR C3368D      ; Hide 4 lines
         BRA C33671      ; Anim timer -1

; Fork: Start ummasking
C33650:  INC $3649,X     ; Mode +1
         INC $33CA,X     ; Scanline +1

; Mode 2: Print 4 lines
C33658:  LDA $45         ; Menu flags
         BIT #$08        ; Got go-signal?
         BEQ C33673      ; Exit if not
         CLC             ; Preset to dequeue
         LDA $22         ; Timer expired?
         BEQ C33674      ; Dequeue if so
         LDA $33CA,Y     ; Scanline
         TAX             ; Index it
         LDA #$17        ; On MS: BGs+OBJ
         SEC             ; Mode: Down
         JSR C3368_      ; Unhide 4 lines
C33671:  DEC $22         ; Anim timer -1
C3367_:  STA $33CA,Y     ; Save scanline
C33673:  SEC             ; Set to requeue
C33674:  RTS

; Enable or disable desired layers on 4 scanlines
C3368D:  CLC             ; Mode: Up
C3368_:  PHY             ; Save Y
         LDY #$0004      ; Loop counter
.loop    STA $9A09,X     ; Line's MS desig
         BCS .down       ; If mode = down
         DEX             ; Scanline -1
         DEX             ; Scanline -1
.down    INX             ; Scanline +1
         DEY             ; Counter -1
         BNE .loop       ; Loop till 0
         PLY             ; Restore Y
         TXA             ; Line number
         RTS

; 65: Shift screen left or right
C336E7:  JSR C31D8B      ; Handle timer
C336EF:  REP #$21        ; 16-bit A; C-
         LDA $35         ; BG1 X-Pos
         ADC $9C         ; Add scroll speed
         TAY             ; Put sum in Y
         SEP #$20        ; 8-bit A
         JMP C3690X      ; Set BGs' X-Pos

; Update arrow in Config menu
C33980:  BNE C339AB      ; If mode 1

; Mode 0: Initialize arrow
C33988:  JSR initob1     ; Ptr HB: C3
         JSR C339AB      ; Set ptr LBs
         JSR C3093D      ; X: 120
         LDA #$18        ; Y: 24
         STA $344A,X     ; Set arrow's
         JSR C31206      ; Set pose timer

; Mode 1: Sustain arrow
C339AB:  TXY             ; Queue index
         TDC             ; Arrow: Down
         LDA $4A         ; On first page?
         BEQ C339B6      ; Branch if so
         LDA #$02        ; Arrow: Up
C339B6:  TAX             ; Set index
         REP #$20        ; 16-bit A
         LDA.L C339C7,X  ; Anim table ptr
C339BD:  LDX $2D         ; Queue index
         JMP C3C190      ; Set OAM; requeue

; 0E: Scroll to Config page 2
C33A00:  LDA $20         ; Timer expired?
         BEQ C33A15      ; Finalize if so
         LDA #$10        ; Loop counter
         LDY $37         ; BG1 Y-Pos
.loop    INY             ; BG1 Y-Pos +1
         DEC A           ; Counter -1
         BNE .loop       ; Loop till 0
         BRA C33A2_      ; Save Y-Pos
C33A15:  INC $26         ; Next: Sustain menu
         JMP C339EA      ; Switch screens

; 10: Scroll to Config page 1
C33A21:  LDA $20         ; Timer expired?
         BEQ C33A36      ; Finalize if so
         LDA #$10        ; Loop counter
         LDY $37         ; BG1 Y-Pos
.loop    DEY             ; BG1 Y-Pos -1
         DEC A           ; Counter -1
         BNE .loop       ; Loop till 0
C33A2_:  STY $37         ; Save Y-Pos
         RTS             ; Exit
C33A36:  DEC $26         ; Next: Sustain menu
         JSR C32406      ; Nav data; BG1 Y
         LDA #$08        ; Row: "Controller"
         STA $4E         ; Set cursor row
         JMP C33861      ; Relocate cursor

; Set Window 2 positions per scanline for Config menu
C33A4C:  LDY #C33A7A     ; Src: C3/3A7A
         JMP C348AB      ; Set up HDMA

; Window 2 positions per scanline for Config menu
C33A7A:  db $27,$FF,$FF  ; 39 lines: 255, 255
         db $50,$08,$F7  ; 80 lines: 8, 247
         db $50,$08,$F7  ; 80 lines: 8, 247
         db $10,$FF,$FF  ; 16 lines: 255, 255
         db $00          ; End

; Set source for uploading skin graphics
C33B30:  AND #$0F        ; Skin number
         LDY #$0380      ; Skin data size
         JSR mul16       ; Skin x size
         STX $16         ; Set as src LBs
         LDA #$ED        ; Bank: ED
         STA $18         ; Set src HB
         LDY #$0400      ; Bytes: 1024
         STY $12         ; Set data size
         RTS

; Jump table for updating game options
C33D43:  dw C33D61       ; Bat.Mode
         dw C33D7A       ; Bat.Speed
         dw C33DAB       ; Msg.Speed
         dw C33DE8       ; Cmd.Set
         dw C33E01       ; Gauge
         dw C33E1A       ; Sound
         dw C33E4E       ; Cursor
         dw C33E6D       ; Reequip
         dw C33E86       ; Controller
C33D55:  dw C33E9F       ; Mag.Order
         dw C33ECD       ; Window
         dw C33F01       ; Viewed color
         dw C33F3C       ; R
         dw C33F3C       ; G
         dw C33F3C       ; B

; Update Bat.Speed
C33D7A:  LDA $1D4D       ; Game options
         AND #$07        ; Get Bat.Speed
         TRB $1D4D       ; Clear in config
         BCC C33D95      ; If pushing left
         CMP #$05        ; On "6" now?
         BEQ C33D9E      ; Retain if so
         INC A           ; Speed +1
         INC A           ; Speed +1
C33D95:  BEQ C33D9E      ; Retain if on "1"
         DEC A           ; Speed -1
C33D9E:  TSB $1D4D       ; Update config
C33BB7:  LDY #C3496E     ; Text pointer
         JSR draw28      ; Draw 1-6 in gray
         LDX #$3A24      ; Tilemap ptr (-1)
         LDA $1D4D       ; Game options
         AND #$07        ; Get Bat.Speed
         BRA C33C24      ; Draw user value

; Update Msg.Speed
C33DAB:  PHP             ; Save direction
         LDA $1D4D       ; Game options
         AND #$70        ; Get Msg.Speed
         TRB $1D4D       ; Clear in config
         JSR C3CC33      ; Now 0-5
         PLP             ; Pushing left?
         BCC C33DCA      ; Branch if so
         CMP #$05        ; On "6" now?
         BEQ C33DD3      ; Retain if so
         INC A           ; Speed +1
         INC A           ; Speed +1
C33DCA:  CMP #$00        ; On "1" now?
         BEQ C33DD3      ; Retain if so
         DEC A           ; Speed -1
C33DD3:  JSR x16         ; Speed x16
         TSB $1D4D       ; Update config
C33BF2:  LDY #C3497C     ; Text pointer
         JSR draw28      ; Draw 1-6 in gray
         LDX #$3AA4      ; Tilemap ptr (-1)
         LDA $1D4D       ; Game options
         AND #$70        ; Get Msg.Speed
         JSR C3CC33      ; Now 0-5
C33C24:  JMP C340E_      ; Draw user speed

; Update Bat.Mode
C33D61:  LDA #$08        ; Bat.Mode bit
         TRB $1D4D       ; Mode: Active
         BCC C33B8C      ; If pushing left
         TSB $1D4D       ; Mode: Wait
C33B8C:  PEA $2820       ; Active mode pals
         LDA $1D4D       ; Game options
         AND #$08        ; Wait mode?
         BEQ C33B8_      ; Branch if not
         PLY             ; Unstack palettes
         PEA $2028       ; Wait mode pals
C33B8_:  PLA             ; Active's palette
         LDY #C349F1     ; Text pointer
         JSR C302F_      ; Draw "Active"
         LDY #C34918     ; Text pointer
         BRA C33CA7      ; Draw "Wait"

; Update Cmd.Set mode
C33DE8:  TRB $1D4D       ; Mode: Window
         BCC C33C33      ; If pushing left
         TSB $1D4D       ; Mode: Short
C33C33:  PEA $2028       ; Short mode pals
         LDA $1D4D       ; Short mode?
         BMI C33C3_      ; Branch if so
         PLY             ; Unstack palettes
         PEA $2820       ; Window mode pals
C33C3_:  PLA             ; Window's palette
         LDY #C349FA     ; Text pointer
         JSR C302F_      ; Draw "Window"
         LDY #C3492D     ; Text pointer
         BRA C33CA7      ; Draw "Short"

; Update gauge mode
C33E01:  TRB $1D4E       ; Gauge: On
         BCC C33C5C      ; If pushing left
         TSB $1D4E       ; Gauge: Off
C33C5C:  PEA $2820       ; On mode pals
         LDA $1D4E       ; Gauges on?
         BPL C33C5_      ; Branch if so
         PLY             ; Unstack palettes
         PEA $2028       ; Off mode pals
C33C5_:  PLA             ; On's palette
         LDY #C34935     ; Text pointer
         JSR C302F_      ; Draw "On"
         LDY #C3493A     ; Text pointer
         BRA C33CA7      ; Draw "Off"

; Update sound mode
C33E1A:  LDA #$20        ; Sound option bit
         TRB $1D4E       ; Option: Stereo
         BCS C33E2F      ; If pushing right
         TDC             ; Volume: Stereo
         BRA C33E2_      ; Branch below
C33E2F:  TSB $1D4E       ; Option: Mono
         LDA #$FF        ; Volume: Mono
C33E2_:  JSR C33E3F      ; Adjust volume
         JSR C30EA3      ; Sound: Cursor
C33C85:  PEA $2820       ; Stereo mode pals
         LDA $1D4E       ; Game options
         AND #$20        ; Stereo?
         BEQ C33C8_      ; Branch if so
         PLY             ; Unstack palettes
         PEA $2028       ; Mono mode pals
C33C8_:  PLA             ; Stereo's palette
         LDY #C34940     ; Text pointer
         JSR C302F_      ; Draw "Stereo"
         LDY #C34949     ; Text pointer
C33CA7:  PLA             ; Mono's palette
         JMP C302F_      ; Draw "Mono"

; Update Cursor mode
C33E4E:  LDA #$40        ; Cursor bit
         TRB $1D4E       ; Mode: Reset
         BCC C33CB0      ; If pushing left
         TSB $1D4E       ; Mode: Memory
         JSR C37068      ; Reset positions
C33CB0:  PEA $2820       ; Reset mode pals
         JSR b6_1D4E     ; Memory mode?
         BPL C33CB_      ; Branch if not
         PLY             ; Unstack palettes
         PEA $2028       ; Memory mode pals
C33CB_:  PLA             ; Reset's palette
         LDY #C34A03     ; Text pointer
         JSR C302F_      ; Draw "Reset"
         LDY #C34950     ; Text pointer
         BRA C33CA7      ; Draw "Memory"

; Update Reequip mode
C33E6D:  LDA #$10        ; Reequip bit
         TRB $1D4E       ; Mode: Optimum
         BCC C33CDB      ; If pushing left
         TSB $1D4E       ; Mode: Empty
C33CDB:  PEA $2820       ; Optimum mode pals
         LDA $1D4E       ; Game options
         AND #$10        ; Optimum mode?
         BEQ C33CD_      ; Branch if so
         PLY             ; Unstack palettes
         PEA $2028       ; Empty mode pals
C33CD_:  PLA             ; Optimum's palette
         LDY #C34959     ; Text pointer
         JSR C302F_      ; Draw "Optimum"
         LDY #C34A0B     ; Text pointer
         BRA C33CA7      ; Draw "Empty"

; Update Controller mode
C33E86:  TRB $1D54       ; Mode: Single
         BCC C33D06      ; If pushing left
         TSB $1D54       ; Mode: Multiple
C33D06:  PEA $2820       ; Single mode pals
         LDA $1D54       ; Single mode?
         BPL C33D0_      ; Branch if so
         PLY             ; Unstack palettes
         PEA $2028       ; Multiple mode pals
C33D0_:  PLA             ; Single's palette
         LDY #C34A13     ; Text pointer
         JSR C302F_      ; Draw "Single"
         LDY #C34963     ; Text pointer
         BRA C33CA7      ; Draw "Multiple"

; Update Mag.Order
C33E9F:  LDA $1D54       ; Game options
         AND #$07        ; Get Mag.Order
         TRB $1D54       ; Clear in config
         BCC C33EB7      ; If pushing left
         CMP #$05        ; On "6" now?
         BEQ C33EC0      ; Retain if so
         INC A           ; Order +1
         INC A           ; Order +1
C33EB7:  BEQ C33EC0      ; Retain if "1"
         DEC A           ; Order -1
C33EC0:  TSB $1D54       ; Update config

; Fork: Draw Mag.Order settings
C33FFD:  LDY #C34AA5     ; Text pointer
         JSR draw28      ; Draw 1-6 in gray
         LDX #$41A4      ; Tilemap ptr (-1)
         LDA $1D54       ; Game options
         AND #$07        ; Get Mag.Order
         PHA             ; Memorize it
         JSR C340E_      ; Draw user value

; Fork: Draw all three Mag.Order spell types
C3403B:  PLA             ; Mag.Order
         XBA             ; Put in B
         LDA #$03        ; Multiplier
         JSR C350F_      ; X = Order x3
         LDY #$0002      ; Strings: 3
C3404C:  PHY             ; Save counter
         PHX             ; Save index
         PHX             ; Save index
         REP #$20        ; 16-bit A
         TYA             ; Strings left
         ASL A           ; Double it
         TAX             ; Index it
         LDA.L C340CC,X  ; Tilemap ptr
         JSR C3BAB3      ; Set position
         JSR wm9E8B      ; WRAM: 7E/9E8B
         PLX             ; Letter index
         LDY #$0008      ; Letters: 8
;        TDC             ; Clear A
         LDA.L C340D2,X  ; Text pointer
         TAX             ; Index it
.loop    LDA.L C34AD3,X  ; Letter
         STA $2180       ; Add to string
         INX             ; Point to next
         DEY             ; One less left
         BNE .loop       ; Loop till last
         JSR C33502      ; Draw string
         PLX             ; String index
         PLY             ; Strings left
         INX             ; Point to next
         DEY             ; One less left
         BPL C3404C      ; Loop till last
         RTS

; Handle wallpaper selection
C33ECD:  JSR getskin     ; Get wallpaper
         TRB $1D4E       ; Clear in config
         BCC C33EE5      ; If pushing left
         CMP #$07        ; On "8" now?
         BEQ C33EEE      ; Retain if so
         INC A           ; Skin number +1
         INC A           ; Skin number +1
C33EE5:  BEQ C33EEE      ; Retain if on "1"
         DEC A           ; Skin number -1
C33EEE:  TSB $1D4E       ; Set new skin
         JSR C340EA      ; Redraw values
         JSR C33A87      ; Refresh skin
         BRA C33F39      ; Draw RGB info

; Change viewed color in Config
C33F01:  PHP             ; Save direction
         JSR skncol      ; Viewed color
         TRB $1D54       ; Clear in Config
         LSR A           ; Put in b2-b4
         LSR A           ; Put in b1-b3
         LSR A           ; Now 0-7
         PLP             ; Pushing left?
         BCC C33F1C      ; Branch if so
         CMP #$07        ; On last color?
         BEQ C33F25      ; Retain if so
         INC A           ; Color number +1
         INC A           ; Color number +1
C33F1C:  CMP #$00        ; Editing font?
         BEQ C33F25      ; Retain if so
         DEC A           ; Color number -1
C33F25:  ASL A           ; Color number x2
         ASL A           ; Color number x4
         ASL A           ; Color number x8
         TSB $1D54       ; Update config

; Fork: Redraw palette, arrow, and RGB info
C33F36:  LDA #$3C        ; Text palette: 7
         LDY #C34AC9     ; Text pointer
         JSR C302F_      ; Draw palette
         LDY #$44F5      ; Tilemap ptr
         JSR C3BAB_      ; Set position
         JSR C3630_      ; Erase arrow
         PEA $2820       ; Font mode pals
         TDC             ; Clear A
         JSR skncol      ; Viewed color
         BEQ C34171      ; Branch if font
         LSR A           ; Put in b2-b4
         LSR A           ; Put in b1-b3
         TAX             ; Index it
         REP #$20        ; 16-bit A
         LDA #$20D4      ; Arrow tile data
         STA $7E44F3,X   ; Put in tilemap
         SEP #$20        ; 8-bit A
         PLY             ; Unstack palettes
         PEA $2028       ; Window mode pals
C34171:  PLA             ; Font's palette
         LDY #C34AB3     ; Text pointer
         JSR C302F_      ; Draw "Font"
         PLA             ; Window's palette
         LDY #C34ABA     ; Text pointer
         JSR C302F_      ; Draw "Window"
C33F39:  JMP C341C3      ; Draw RGB info

; Update component of font or skin color
C33F3C:  JSR C341FE      ; Separate RGB
         JSR C3BFC2      ; X = Cursor slot
         LDA.L colptr-3,X; Color index
         TAX             ; Index it
         LDA $0B         ; Semi-auto keys
         LSR A           ; Pushing right?
         LDA $E0,X       ; Color component
         BCS C33F4F      ; Branch if right
         BEQ C33F55      ; Branch if 0
         DEC A           ; Component -1
         DEC A           ; Component -1
C33F4F:  CMP #$1F        ; Component = 31?
         BEQ C33F55      ; Branch if so
         INC A           ; Component +1
C33F55:  STA $E0,X       ; Save component
         JSR C34221      ; Merge RGB

; Fork: Apply player's skin or font colors
C33FC4:  JSR colrptr     ; Color index
         TAY             ; Index it
         BEQ C33FF2      ; Branch if font
         REP #$20        ; 16-bit A
         LDA $9A         ; Updated color
         STA $1D55,Y     ; Set as user's
         JSR C33FAD      ; Recolor skin
         BRA C33FFA      ; Redraw RGB info

; Fork: Font
C33FF2:  LDY $9A         ; Updated color
C33FF4:  STY $1D55       ; Put in options
         JSR C33F99      ; Put in palettes
C33FFA:  JMP C341C3      ; Redraw RGB info

; Pointers to color components
colptr:  db $00          ; Red
         db $02          ; Green
         db $01          ; Blue

; Apply player's font color
C33F99:  REP #$20        ; 16-bit A
         LDA $1D55       ; User font color
C33F9E:  STA $7E304F     ; Set for FWF
         STA $7E3073     ; Set VWF color A
         STA $7E3077     ; Set VWF color B
         SEP #$20        ; 8-bit A
         RTS

; Apply player's skin colors
C33B5B:  AND #$0F        ; Skin number
         JSR C350F5      ; Times 15
         BRA C33FA_      ; Copy colors

; Apply player's skin colors after changes
C33FAD:  LDY #$312B      ; Dst: 7E/312B
C33FA_:  REP #$21        ; 16-bit A; C-
         TXA             ; Color index
         ADC #$1D57      ; Index + src adr
         TAX             ; Put in X
         SEP #$20        ; 8-bit A
         LDA #$7E        ; Src bank: 7E
         PEA $000D       ; Colors: 7
         JMP C30F91      ; Copy colors

; Draw wallpaper values
C340EA:  LDY #C34A93     ; Text pointer
         JSR draw28      ; Draw 1-8 in gray
         LDX #$43A4      ; Tilemap ptr (-1)
         JSR getskin     ; Get wallpaper
C340E_:  TAY             ; Memorize it
         ASL A           ; x2
         ASL A           ; x4
.loop    INX             ; Tilemap ptr +1
         DEC A           ; Counter -1
         BPL .loop       ; Loop till < 0
         TYA             ; Skin number
C3410F:  CLC             ; Prepare ADC
         ADC.B #!num0+1  ; Convert to text
         STA $F9         ; Add to string
         STZ $FA         ; End string
         LDY #$00F7      ; Src: 00/00F7
         TDC             ; Source bank
         STX $F7         ; Set position
         JSR C38085      ; Color: User's
         JMP C302FB      ; Draw user value

; Draw RGB gauges and values in Config menu
C341C3:  JSR txt30       ; Color: Gray
         LDX #C34A22     ; Text ptrs loc
         JSR drawx3      ; Draw empty gauges
         JSR colrptr     ; Color index
         TAX             ; Index it
         LDY $1D55,X     ; Viewed color
C341EA:  STY $9A         ; Memorize it

; Fork: Draw value and fill gauge for blue component
C34263:  JSR C341FE      ; Separate RGB
         LDY #$462F      ; Gauge position
         LDA $E1         ; Blue value
         LDX #$4629      ; Value position
         JSR C34276      ; Draw text

; Fork: Draw value and fill gauge for green component
         LDY #$45AF      ; Gauge position
         LDA $E2         ; Green value
         LDX #$45A9      ; Value position
         JSR C34276      ; Draw text

; Fork: Draw value and fill gauge for red component
         LDY #$452F      ; Gauge position
         LDA $E0         ; Red value
         LDX #$4529      ; Value position

; Fork: Draw value and fill gauge for color component
C34276:  PHA             ; Save value
         PHA             ; Do it again
         PHX             ; Save position
         PHA             ; Save value
         JSR C3BAB_      ; Set gauge pos
         JSR C38085      ; Color: User's
         PLA             ; 0-31 value
         PLX             ; Text position
         JSR C304B_      ; Draw 2 digits
         JSR wm9E8B      ; WRAM: 7E/9E8B
         TDC             ; Clear A
         PLA             ; Color value
         LSR A           ; ÷2
         LSR A           ; ÷4
         TAX             ; Set counter
         BEQ C3429C      ; Branch if 0
C34294:  LDA.B #!bar8    ; Full tile char
         STA $2180       ; Add to string
         DEX             ; One less tile
         BNE C34294      ; Loop till last
C3429C:  JSR txt30       ; Text color: Gray
         PLA             ; Color component
         AND #$03        ; Extremity size
         ASL A           ; Double it
         ADC.B #!bar0    ; Convert to tile
         JSR C35AB9      ; Draw gauge
         JMP C341FE      ; Reseparate RGB

; Get viewed color's index for Config menu
colrptr: SEP #$20        ; 8-bit A
         JSR getskin     ; Get wallpaper
         JSR C350F5      ; Compute index
         JSR skncol      ; Viewed color
         BEQ .exit       ; Exit if font
         LSR A           ; Put in b2-b4
         LSR A           ; Put in b1-b3
         ADC $4216       ; Add skin x15
.exit    RTS

; Separate RGB components for color in Config menu
C341F_:  STA $9A         ; Save color
C341FE:  SEP #$20        ; 8-bit A
         LDA $9B         ; Color HB
         AND #$7F        ; Clear flag
         LSR A           ; ÷2
         LSR A           ; ÷4
         XBA             ; Put in B
         LDA $9A         ; Color LB
         AND #$1F        ; Get red
         REP #$20        ; 16-bit A
         STA $E0         ; Save R&B
         LDA $9A         ; Color
         JSR div32       ; ÷32
         SEP #$20        ; 8-bit A
         AND #$1F        ; Get green
         STA $E2         ; Memorize it
         RTS

; Merge RGB components of updated color
C34221:  ASL $E1         ; Blue x2
         ASL $E1         ; Blue x4
         TDC             ; Clear A
         LDA $E2         ; Green
         JSR x32r        ; x32
         ORA $E0         ; Add R&B
         STA $9A         ; Memorize it
C3423C:  RTS

; 47: Initialize Cmd.Set menu
C342C2:  JSR C3352F      ; Reset/Stop stuff
         JSR C36A28      ; Clear BG2 map A
         LDX #C34490     ; C3/4490
         JSR C3034Y      ; Draw windows
         JSR bg1b_cl     ; Clear BG1 maps
         JSR bg3b_cl     ; Clear BG3 maps
         LDY #C34AF1     ; Text pointer
         JSR draw30      ; Draw "Arrange"
         JSR C344B4      ; Draw other text
         JSR mvmapx3     ; Upload tilemaps
         JSR C3466F      ; Set to shift text
         JSR C345B6      ; Load navig data
         JSR C345BF      ; Relocate cursor
         LDA #$48        ; Queue: Pick char
         JMP C346A_      ; Queue cursor OAM

; 48: Handle Cmd.Set menu member selection
C342DF:  JSR C345BC      ; Handle D-Pad
         LDA $08         ; Pushing A?
         BPL C34310      ; Branch if not
;        TDC             ; Clear A
         LDA $4B         ; Selected member
         CMP #$04        ; Chose Arrange?
         BEQ C34326      ; Sort if so
         STA $5E         ; Save member slot
         TAX             ; Index it
         LDA $69,X       ; Nobody there?
         BMI C3430A      ; Fail if so
         JSR C30EB2      ; Sound: Click
         INC $26         ; Next: Cmd choice
         LDY #C3464A     ; C3/464A
         JSR C305FE      ; Load navig data
         JMP C345DA      ; Relocate cursor

; Fork: Play buzzer
C3430A:  JMP C3B66F      ; Buzzer + Mosaic

; Fork: Handle B and Start
C34310:  LDA $09         ; No-autofire keys
         BIT #$90        ; Using Start or B?
         BEQ C3423C      ; Exit if not
         JMP C346DF      ; Exit submenu

; Fork: Reset command order
C34326:  JSR C30EB2      ; Sound: Click

; Fork: Reset all battle commands
C343F3:  LDX #$0006      ; Member slot: 4
.actor   PHX             ; Memorize slot
         LDY $6D,X       ; Actor's address
         BEQ .next       ; Skip if nobody
         JSR gogoch2     ; Gogo?
         BEQ .next       ; Skip if so
         XBA             ; Put in B
         LDA #$16        ; Actor data size
         JSR C350F_      ; Actor x size
         LDA #$04        ; Commands: 4
.cmd     PHA             ; Save counter
         LDA $ED7CA2,X   ; Command
         STA $0016,Y     ; Set actor's
         INX             ; Src index +1
         INY             ; Dst index +1
         PLA             ; Commands left
         DEC A           ; One less left
         BNE .cmd        ; Loop till last
.next    PLX             ; Member index
         DEX             ; Index -1
         DEX             ; Index -1
         BPL .actor      ; Loop till last

; Fork: Draw actor names and commands
C344B4:  LDX #$0003      ; Member slot: 4
.actor   PHX             ; Memorize slot
         LDA $69,X       ; Actor in slot
         BMI .next       ; Skip if none
         JSR C32CE5      ; Load gear data
         REP #$20        ; 16-bit A
         TXA             ; Member slot
         JSR C34352      ; Actor's address
         LDA.L actpos,X  ; Actor name pos
         PHA             ; Memorize it
         TXA             ; Member slot x2
         ASL A           ; Member slot x4
         ASL A           ; Member slot x8
         TAX             ; Index it
         LDY #$0004      ; Positions: 4
.cmd     LDA.L cmdpos,X  ; Command position
         PHA             ; Memorize it
         INX             ; ROM index +1
         INX             ; ROM index +1
         DEY             ; One less left
         BNE .cmd        ; Loop till last
         SEP #$20        ; 8-bit A
         JSR C38085      ; Color: User's
         PLY             ; Text position
         JSR C34598      ; Draw command 1
         PLY             ; Text position
         JSR C3459E      ; Draw command 2
         PLY             ; Text position
         JSR C345A5      ; Draw command 3
         PLY             ; Text position
         JSR C345AD      ; Draw command 4
         JSR txt30       ; Color: Blue
         PLY             ; Text position
         JSR C334CF      ; Draw actor name
.next    PLX             ; Member slot
         DEX             ; Member slot -1
         BPL .actor      ; Loop till last
         JMP C30F39      ; Queue text upload

; 49: Handle Cmd.Set menu command choice 1
C34338:  JSR C34602      ; Handle D-Pad
         LDA $08         ; Pushing A?
         BPL C34359      ; Branch if not
         JSR C30EB2      ; Sound: Click
         LDA $4B         ; Cursor slot
         STA $28         ; Set blinker's
         JSR C32F06      ; Create blinker
         INC $26         ; Next: Choice 2
         TDC             ; Clear A
         LDA $5E         ; Selected member
C34352:  ASL A           ; Double it
C34353:  TAX             ; Index it
C34354:  LDY $6D,X       ; Actor's address
         STY $67         ; Memorize it
C34358:  RTS

; Fork: Handle B
C34359:  LDA $09         ; Pushing B?
         BPL C34358      ; Exit if not
         DEC $26         ; Next: Pick char
         JSR C345B6      ; Load navig data
         JSR C31EB_      ; SFX + Old cur pos
         JMP C345BF      ; Relocate cursor

; 4A: Handle Cmd.Set menu command choice 2
C34376:  JSR C34602      ; Handle D-Pad
         LDA $08         ; Pushing A?
         BPL C343BD      ; Branch if not
         JSR C30EB2      ; Sound: Click
         LDX $67         ; Actor's address
         DEX             ; Subtract 1
         TXY             ; Copy to Y
         LDA $4B         ; Cursor's slot
.cmd2    INX             ; Cmd2 ptr +1
         DEC A           ; Cursor slot -1
         BPL .cmd2       ; Loop till < 0
         LDA $28         ; Blinker's slot
.cmd1    INY             ; Cmd1 ptr +1
         DEC A           ; Blinker slot -1
         BPL .cmd1       ; Loop till < 0
         LDA $16,X       ; Second command
         PHA             ; Memorize it
         LDA $0016,Y     ; First command
         STA $16,X       ; Replace second
         PLA             ; Second command
         STA $0016,Y     ; Replace first
         JSR C344B4      ; Redraw commands
         BRA C343C6      ; Next: Choice 1

; Fork: Handle B
C343BD:  LDA $09         ; Pushing B?
         BPL C343CF      ; Exit if not
         JSR C30EA9      ; Sound: Cursor
C343C6:  DEC $26         ; Next: Choice 1
C343C_:  LDA #$05        ; Blinker: Off
         TRB $46         ; Set menu flag
C343CF:  RTS

; Draw member's command A
C34598:  JSR C33519      ; Set pos/WRAM/Y
         BRA C35EE1      ; Draw command

; Draw member's command B
C3459E:  JSR C33519      ; Set pos/WRAM/Y
         BRA C345B2      ; Branch below

; Draw member's command C
C345A5:  JSR C33519      ; Set pos/WRAM/Y
         BRA C345B1      ; Branch below

; Draw member's command D
C345AD:  JSR C33519      ; Set pos/WRAM/Y
         INY             ; Point to cmd 2
C345B1:  INY             ; Point to cmd 3
C345B2:  INY             ; Point to cmd 4

; Fork: Draw command's name, apply modifiers
C35EE1:  JSR C3612C      ; Apply modifiers
C35EE4:  BMI C35F0C      ; Blank if no cmd
         PHA             ; Memorize command
         JSR C35F25      ; Pick a palette
         LDA.B #!cmd_np!h; Text ptr HB
         LDY.W #!cmd_np  ; Text ptr LBs
         LDX.w #!cmd_ns  ; Letters: 7
C35EE_:  JSR C3532Z      ; Set src, size
         JMP C334FX      ; Draw cmd name

; Fork: Blank command slot
C35F0C:  LDX.W #!cmd_ns  ; Spaces: 7
         JMP C357F3      ; Draw 7 spaces

; Handle D-Pad for Cmd.Set menu's command list
C34602:  LDX #$0003      ; D-Pad key index
         LDA $0B         ; Semi-auto keys
         ASL A           ; Put D-Pad in b1-4
         ASL A           ; Put D-Pad in b2-5
         ASL A           ; Put D-Pad in b3-6
.loop    ASL A           ; Current key set?
         BPL .next       ; Skip if not
         PHA             ; Save keys
         JSR C30EA3      ; Sound: Cursor
         LDA.L cmdkeys,X ; Intended slot
         STA $4E         ; Set cursor slot
         JSR C30EA3      ; Sound: Cursor
         PLA             ; Pushed keys
.next    DEX             ; Key index -1
         BPL .loop       ; Loop till < 0
C345DA:  TDC             ; Clear A
         LDA $5E         ; Member slot
         ASL A           ; x2
         ASL A           ; x4
         ASL A           ; x8
         REP #$21        ; 16-bit A; C-
         ADC #C3464F     ; Cursor data loc
         TAY             ; Put sum in Y
         JMP C30640      ; Relocate cursor

; Cursor slots for Cmd.Set menu's command list
cmdkeys: db $02          ; Right
         db $01          ; Left
         db $03          ; Down
         db $00          ; Up

; Set to condense text in Cmd.Set menu
C3466F:  LDY #C3468E     ; Src: C3/468E
         JMP C3C01B      ; Set up HDMA

; 4B: Initialize Controller menu
C346AD:  JSR C3352F      ; Reset/Stop stuff
         LDX #C348F3     ; C3/48F3
         JSR C30341      ; Draw menu window
         LDY #C34AFB     ; Text pointer
         JSR draw30      ; Draw "Controller"
         JSR C347B4      ; Draw party text
         JSR mvmapx3     ; Upload tilemaps
         JSR C30F39      ; Queue BG3 upload
         JSR C348A8      ; Queue Win2 HDMA
         JSR C315B8      ; Load navig data
         JSR C348E0      ; Relocate cursor
         LDA #$4C        ; C3/46CA
C346A_:  JSR C32AA5      ; Queue: Sustain menu
         JMP C307B0      ; Queue cursor OAM

; 4C: Sustain Controller menu
C346CA:  JSR C348DD      ; Handle D-Pad
         LDA $09         ; No-autofire keys
         BIT #$90        ; Using Start or B?
         BEQ C346E6      ; Branch if not
C346DF:  LDA #$0D        ; Queue: Config menu
         JMP C3224F      ; Sound + Fade-out

; Fork: Handle left and right
C346E6:  LDA $0B         ; Semi-auto keys
         BIT #$03        ; Left or right?
         BEQ C347B_      ; Exit if not
         LSR A           ; Carry = Right key
         JSR C3B2EC      ; On empty slot?
         BMI C347B_      ; Exit if so
         JSR C30EA3      ; Sound: Cursor
         LDA.L C3A53D,X  ; Joypad bit
         TRB $1D4F       ; Assign Cntlr1
         BCC C347B4      ; If pushing left
         TSB $1D4F       ; Assign Cntlr2

; Fork: Draw party-related text
C347B4:  LDX #$0003      ; Member slot: 4
.actor   LDA $69,X       ; Nodoby in slot?
         BMI .next       ; Skip it if so
         PHX             ; Save slot
         PHX             ; Save slot
         REP #$20        ; 16-bit A
         TXA             ; Member slot
         JSR C34352      ; Get actor's adr
         PLY             ; Member slot
         LDA.L cnt_a,X   ; Actor position
         PHA             ; Memorize it
         LDA.L cnt_b,X   ; Ptr to Cntlr1
         PHA             ; Memorize it
         LDA.L cnt_c,X   ; Ptr to Cntlr2
         PHA             ; Memorize it
         SEP #$20        ; 8-bit A
         TYX             ; Member slot
         LDA $1D4F       ; Assigned pad
         AND.L C3A53D,X  ; Player 1's?
         BEQ .draw       ; Branch if so
         PLX             ; Pull Cntlr2's ptr
         PLY             ; Pull Cntlr1's ptr
         PHX             ; Push Cntlr2's ptr
         PHY             ; Push Cntlr1's ptr
.draw    PLY             ; Text pointer
         JSR draw24      ; Draw grayed pad
         PLY             ; Text pointer
         JSR draw20      ; Draw user's pad
         PLY             ; Text position
         JSR C334CF      ; Draw actor name
         PLX             ; Member slot
.next    DEX             ; Member slot -1
         BPL .actor      ; Loop till < 0
C347B_:  RTS

; Set Window 2 positions per scanline for Controller menu
C348A8:  LDY #C348C7     ; C3/48C7
C348AB:  STY $4352       ; Set src LBs
         LDA #$C3        ; Bank: C3
         STA $4354       ; Set src HB
         LDA #$28        ; $2128, $2129
         STA $4351       ; To Win 2 L+R
         LDA #$01        ; 2Rx1B to PPU
dma5b:   STA $4350       ; Set DMA mode
         LDA #$20        ; Channel: 5
         TSB $43         ; Queue HDMA-5
         RTS

; Window 2 positions per scanline for Controller menu
C348C7:  db $27,$FF,$FF  ; 255, 255
         db $30,$08,$F7  ; 8, 247
         db $68,$FF,$FF  ; 255, 255
         db $10,$08,$F7  ; 8, 247
         db $10,$FF,$FF  ; 255, 255
         db $00          ; End

; Draw Skills menu, disable individual MP costs in Magic menu
C34C80:  STZ $9E         ; No MP by Magic
         JSR bg1_2x1     ; BG1: 64x32 at $00
         JSR bg2b_cl     ; Clear BG2 maps
         LDX #C34D98     ; C3/4D98
         JSR C3034Y      ; Draw windows
         JSR C34D27      ; Draw actor info
         JSR bg3c_cl     ; Clear BG3 maps
         LDY #$80CD      ; Dest: 7E/80CD
         JSR C3A740      ; Build desc map
         LDX #$0006      ; 1st: Dance
.loop    PHX             ; Save skill index
         LDA $79,X       ; Text palette
         STA $29         ; Set attributes
         REP #$20        ; 16-bit A
         TXA             ; Skill index
         ASL A           ; Double it
         TAX             ; Reindex it
         LDA.L skillnm,X ; Text pointer
         TAY             ; Put it in Y
         JSR C302F9      ; Draw skill name
         PLX             ; Skill index
         DEX             ; Skill index -1
         BPL .loop       ; Loop till < 0
         JMP mvmapx3     ; Upload tilemaps

; Equip and draw selected esper
C32929:  JSR C34EDD      ; Actor's address
         LDA $99         ; Chosen esper
         STA $001E,Y     ; Assign to actor
         BRA C34F08      ; Draw it by actor

; Draw actor info in Skills menu
C34D27:  JSR bg1b_cl     ; Clear BG1 maps
         JSR txt24       ; Color: Blue
         LDX #C35C81     ; Text ptrs loc
         JSR drawx3      ; Draw "LV/HP/MP"
         JSR C34EDD      ; Actor's address
         STY $67         ; Memorize it
         LDY #$42DD      ; Class/KO pos
         LDX #$4F50      ; Icon position
         JSR C3625_      ; Display status
         LDX #C35CD_     ; Text ptrs loc
         JSR C39063      ; Draw both "/"
         LDX #C34F12     ; Coords tbl ptr
         JSR C30C6C      ; Draw LV, HP, MP
C34F08:  JSR C38085      ; Color: User's
         LDY #$421D      ; Text position

; Fork: Draw equipped esper
C334E6:  JSR C33519      ; Set pos/WRAM/Y
         LDA $001E,Y     ; Actor's esper
         BPL C334F0      ; Branch if any
         LDX.W #!esp_ns  ; Spaces: 8
         JMP C357F3      ; Draw 8 spaces
C334F0:  PHA             ; Save esper
         JSR C354FA      ; Define source
C334FX:  PLA             ; Esper
C334FY:  JSR C3846_      ; Load its name
C33502:  STZ $2180       ; End string
         JMP C38847      ; Draw string

; Set to condense text in Skills menu or submenu
C34DBC:  JSR C34E2D      ; Load BG1 V-Data
         LDX #$000C      ; H-Pan data index
C34DDF:  LDA.L C34E72,X  ; BG1 H-Pan data
         STA $7E9A09,X   ; Save in RAM
         DEX             ; Index -1
         BPL C34DDF      ; Loop till done
         LDY #C34E26     ; BG3v src
         JMP dma7a       ; Queue BG1H/BG3V

; Load vertical shift values for BG1 text in skill menus
C34E2D:  LDX #$005D      ; Data index
C34E2F:  LDA.L C34E7F,X  ; V-Shift data
         STA $7E9849,X   ; Save in RAM
         DEX             ; Index -1
         BPL C34E2F      ; Loop till < 0
         LDX #$0045      ; Data index
C34E3D:  JSR topbg1      ; BG1 top row
         ADC.L C34E91+1,X; Add skill V-Data
         STA $7E985C,X   ; Save in RAM
         SEP #$20        ; 8-bit A
         DEX             ; Index -1
         DEX             ; Index -1
         DEX             ; Index -1
         BPL C34E3D      ; Loop till < 0
         RTS

; Load member's SRAM block address for Skills menu
C34EDD:  JSR ld28x2      ; X = Mbr slot
         BRA C32D10      ; SRAM address

; Get target's SRAM block address
C32D0B:  TDC             ; Clear A
         LDA $4B         ; Cursor slot
C32D0E:  ASL A           ; Double it
         TAX             ; Index it
C32D10:  LDY $6D,X       ; SRAM address
C32D12:  RTS

; Build Magic list based on Mag.Order
C34F1C:  JSR wm9D89      ; WRAM: 7E/9D89
         LDY #$0036      ; Spells: 54
C34F27:  JSR C3C13B      ; Clear list slot
         DEY             ; One less spell
         BNE C34F27      ; Loop till last
         TDC             ; Clear A
         LDA $1D54       ; Game options
         AND #$07        ; Get Mag.Order
         ASL A           ; x2
         ASL A           ; x4
         TAX             ; Index it
;        LDY $00         ; List slot: 1
C34F37:  LDA.L C34F49,X  ; Starting spell
         BMI C32D12      ; If terminator
         PHX             ; Save index
C34F61:  LDX #$0018      ; Black spells: 24
         CMP #$00        ; Base = Fire?
         BEQ C34F78      ; Branch if so
         LDX #$0009      ; White spells: 9
         CMP #$2D        ; Base = Cure?
         BEQ C34F78      ; Branch if so
         LDX #$0015      ; Gray spells: 21
C34F78:  STX $E0         ; Set spells left
C34F7B:  TYX             ; List slot
         JSR st_9D89     ; Add spell there
         INC A           ; Spell number +1
         INY             ; List slot +1
         DEC $E0         ; One less spell
         BNE C34F7B      ; Loop till last
         PLX             ; Spell type index
         INX             ; Index +1
         BRA C34F37      ; Do next type

; Starting spell for each Magic type, sorted based on Mag.Order
C34F49:  db $2D,$00,$18,$FF  ; 1: Cure, Fire, Scan
         db $2D,$18,$00,$FF  ; 2: Cure, Scan, Fire
         db $00,$18,$2D,$FF  ; 3: Fire, Scan, Cure
         db $00,$2D,$18,$FF  ; 4: Fire, Cure, Scan
         db $18,$2D,$00,$FF  ; 5: Scan, Cure, Fire
         db $18,$00,$2D,$FF  ; 6: Scan, Fire, Cure

; Draw a row of two spells in Magic menu
C34F9E:  LDX #$0003      ; X: 3
         JSR C34FA9      ; Draw spell A
         LDX #$0010      ; X: 16
C34FA9:  JSR C34FB5      ; Define source
         JSR C34FC4      ; Draw spell B
         INC $E5         ; Menu slot +1
         RTS

; Draw text in spell slot of Magic menu
C34FC4:  JSR sk_slt1     ; Define position
         LDA $9E         ; Show MP in list?
         BEQ C3503E      ; Branch if not
         JSR C3514D      ; Handle graying
         JSR sk_slt4     ; Spell
         BMI C3501A      ; Blank if none
         JSR C350A2      ; Spell mastery
         BPL C3501A      ; Blank if < 100%

; Fork: Draw spell plus MP cost
C34FEB:  LDA $E0         ; Spell
         PHA             ; Memorize it
         JSR C3846_      ; Load name
         PLA             ; Spell
         JSR mptext      ; Define MP cost
         JSR add3n       ; Add to string
;        JSR C3C13B      ; Blank "%"
         JMP C33502      ; Draw string

; Fork: Blank spell slot
C3501A:  JSR sk_slt4     ; X = Spell slot
         JSR st_9D8_     ; Slot: Empty
         JSR C351B9      ; Disable spell
         LDX.W #!mag_ns+4; Letters: 11
         JMP C357F3      ; Draw 11 spaces

; Fork: Draw spell plus percentage
C3503E:  JSR sk_slt4     ; Spell
         PHA             ; Memorize it
         JSR C3846_      ; Load name
         PLA             ; Spell
         BMI C3501A      ; Blank if none
         JSR C350A2      ; Spell mastery
         BEQ C3501A      ; Blank if 0
         BMI C35088      ; Branch if 100%
         JSR C304E0      ; Turn into text
         JSR C351B9      ; Disable spell
         JSR txt2C       ; Color: Bluish
         LDA.B #!3dot    ; Char: "…"
         JMP C35AA6      ; Complete string

; Fork: Draw spell name only
C35088:  JSR C3514D      ; Handle graying
         LDX #$0004      ; Add 4 spaces
         JMP C357F9      ; Draw string

; Get spell's mastery percentage
C350A2:  STA $E0         ; Save spell
         JSR gogoch1     ; Gogo?
         BEQ C350C5      ; Branch if so
C350AE:  XBA             ; Put actor in B
         LDA #$36        ; Spell list size
         JSR C350F_      ; Actor x size
         DEX             ; Offset value
         LDA $E0         ; Spell number
.loop    INX             ; Index +1
         DEC A           ; Spell num -1
         BPL .loop       ; Loop till < 0
         LDA $1A6E,X     ; Spell mastery
         RTS

; Fork: Test all members for Gogo
C350C5:  LDA #$04        ; Member slot: 4 (+1)
C350C7:  DEC A           ; Member slot -1
         STA $E1         ; Save member slot
         JSR C32D0E      ; Actor's address
         BEQ C350E2      ; Skip if nobody
         LDA $0000,Y     ; Actor in slot
         CMP #$0C        ; Gogo or higher?
         BCS C350E2      ; Skip if so
         JSR C350AE      ; Actor's mastery
         BMI C350EB      ; Exit if 100%
C350E2:  LDA $E1         ; Member slot
         BNE C350C7      ; Loop till last
C350EB:  RTS             ; Mastery: 0

; Compute spell or color data index
C350F5:  XBA             ; Put spell in B
         LDA #$0E        ; Data size
C350F_:  JSR mul8        ; Spell x size
         TAX             ; Index product
         TDC             ; Clear A
         RTS

; Define spell's MP cost
C3510_:  JSR C34EDD      ; Caster's address
         JSR C32D13      ; Spell used
C3510D:  PHA             ; Save spell
         JSR C350F5      ; Compute index
         LDA $C46AC5,X   ; Base MP cost
         TAX             ; Save in X

; Fork: Step Mine
C3511A:  PLA             ; Spell
         CMP #$99        ; Step Mine?
         BNE C3512E      ; Branch if not
         LDA $021C       ; Minutes played
         CMP #$1E        ; Set C if > 29
         LDA $021B       ; Hours played
         ROL A           ; Hours x2 + C
         TAX             ; X = MP cost

; Fork: Economizer
C3512E:  LDA $11D7       ; Gear effects
         ASL A           ; Economizer's?
         BPL C3513A      ; Branch if not
         LDX #$0001      ; MP cost: 1

; Fork: Gold Hairpin
C3513A:  ASL A           ; Hairpin effect?
         BPL C35148      ; Exit if not
         TXA             ; MP cost
         INC A           ; MP cost +1
         LSR A           ; MP cost / 2
         TAX             ; X = MP cost
C35148:  STX $E0         ; Save MP cost
         TXA             ; Put it in A
         RTS

; Enable or disable spell in Magic menu
C3514D:  JSR sk_slt4     ; Spell
         CMP #$2A        ; Warp?
         BNE C35155      ; Branch if not

; Fork: Warp
C3517B:  LDA $0201       ; Area permissions
         BIT #$02        ; Can warp here?
         BEQ C351B9      ; Disable if not
         LDA #$2A        ; Spell number

; Fork: Test field compatibility
C35155:  JSR C350F5      ; Compute index
         LDA $C46AC3,X   ; Spell properties
         LSR A           ; Usable in field?
         BCC C351B9      ; Disable if not
         JSR sk_slt4     ; Spell
         JSR C3510D      ; Define MP cost

; Fork: Test Imp
C35188:  JSR C34EDD      ; Define Y
         LDA $0014,Y     ; Actor's status
         AND #$20        ; Imp?
         BEQ C3519B      ; Branch if not
         JSR sk_slt4     ; Spell
         CMP #$23        ; Imp?
         BNE C351B9      ; Disable if not

; Fork: Test MP
C3519B:  LDA #$20        ; Color: User's
         LDX $0D,Y       ; Actor's MP
         CPX $E0         ; Below cost?
         BCS C351C1      ; Enable if not

; Fork: Disable spell
C351B9:  LDA #$28        ; Color: Gray
C351C1:  PHA             ; Save color
         JSR sk_slt4     ; X = Spell slot
         PLA             ; Spell color
         STA $29         ; Set for text
         STA $7E9E09,X   ; Disable spell
         RTS

; Build list of known skills
C353C_:  TDC             ; Entries: 8
C353C1:  STA $E1         ; Set skill count
         JSR C3D51B      ; Define source
         JSR wm9D89      ; WRAM: 7E/9D89
         LDY $00         ; Pack index: 0
         STZ $E0         ; Skill number: 0
C353CB:  LDX #$0008      ; Skills left: 8
         LDA [$E7],Y     ; 8 known skills
C353D1:  ROR A           ; Got current?
         PHA             ; Save 8 skills
         LDA $E0         ; Skill number
         BCS C353DE      ; Retain if known
         LDA #$FF        ; Skill: None
C353DE:  STA $2180       ; Add to list
         INC $E0         ; Skill number +1
         PLA             ; 8 known skills
         DEX             ; Skills left -1
         BNE C353D1      ; Loop till last
         INY             ; Point to next 8
         DEC $E1         ; One less left
         BPL C353CB      ; Loop till last
         RTS

; Build and draw Rage list
C353A7:  LDA #$1F        ; Rages: 256
         LDY #$1D2C      ; Known Rages adr
         JSR C353C1      ; Build Rage list
         LDA #$08        ; Rows left: 8
C353B0:  PHA             ; Save counter
         JSR C353EE      ; Draw 2 Rages
         JSR e6add2      ; BG1 WR row +2
         PLA             ; Rows left
         DEC A           ; One less left
         BNE C353B0      ; Loop till last
         RTS

; Draw Rage name
C35418:  JSR sk_slt1     ; Rage in slot
         INC A           ; None or Pugs?
         BEQ C3543B      ; Blank if so
         TXA             ; Rage slot
         JMP C334FY      ; Draw Rage
C3543B:  LDX.W #!nme_ns  ; Spaces: 10
C3544F:  JMP C357F3      ; Draw 10 spaces

; Build and draw Lore list
C3523C:  LDA #$02        ; Lores: 24
         LDY #$1D29      ; Known Lores adr
         JSR C353C1      ; Build Lore list
         LDA #$08        ; Lores left: 8
C35245:  PHA             ; Save counter
         JSR C35256      ; Draw name, cost
         INC $E5         ; Lore slot +1
         JSR e6add2      ; BG1 WR row +2
         PLA             ; Lores left
         DEC A           ; One less left
         BNE C35245      ; Loop till last
         RTS

; Draw Lore's name and MP cost
C35256:  JSR C38085      ; Color: User's
         JSR C35266      ; Define source
         LDX #$0003      ; X: 3
         JSR sk_slt1     ; Lore in slot
         BMI C352C0      ; Blank if none
         TXA             ; Lore slot
         JSR C3846_      ; Load name
         LDA $E5         ; Lore slot
         CLC             ; Prepare ADC
         ADC #$8B        ; Get attack ID
         BRA C35535      ; Draw string

; Fork: Blank Lore slot
C352C0:  LDX.W #!lor_ns+4; Letters: 14
         BRA C3544F      ; Draw 14 spaces

; Build and draw esper list
C3546C:  JSR C35486      ; Build esper list
         JSR C383F7      ; Define $E5, $E6
         LDA #$08        ; Rows left: 8
C35475:  PHA             ; Save counter
         JSR C354E3      ; Draw 2 espers
         JSR e6add2      ; BG1 WR row +2
         PLA             ; Rows left
         DEC A           ; One less left
         BNE C35475      ; Loop till last
         RTS

; Build sorted esper list
C35486:  LDA #$03        ; Espers: 32
         LDY #$1A69      ; Owned espers adr
         JSR C353C1      ; Build esper list
         LDX #$001F      ; Esper slot: 32
.loop    JSR sk_slt6     ; Esper in slot
         STA $7E9DED,X   ; Copy to buffer
         JSR st_9D8_     ; Clear src slot
         DEX             ; Esper slot -1
         BPL .loop       ; Loop till < 0

; Fork: Sort esper list
C354C3:  TDC             ; Clear A
         LDX #$001A      ; Buffer slot: 27
C354CD:  PHX             ; Save src slot
         LDA $7E9DED,X   ; Esper in slot
         BMI C354DF      ; Skip if none
         PHA             ; Memorize it
         TAX             ; Index it
         LDA $D1F9B5,X   ; Intended slot
         DEC A           ; Subtract 1
         TAX             ; Index it
         PLA             ; Esper number
         JSR st_9D89     ; Add to new list
C354DF:  PLX             ; Source slot
         DEX             ; Source slot -1
         BPL C354CD      ; Loop till < 0
         RTS

; Draw a row of two esper names with MP cost
C354E3:  LDX #$0003      ; X: 3
         JSR C354EE      ; Draw esper A
         LDX #$0011      ; X: 17
C354EE:  JSR C354FA      ; Define source
         JSR C35509      ; Draw esper B
         INC $E5         ; Esper slot +1
         RTS

; Draw esper's name and MP cost
C35509:  JSR sk_slt1     ; Esper in slot
         BMI C357F0      ; Blank if none
         PHA             ; Memorize it
         JSR C35574      ; Choose palette
         JSR C3846_      ; Load name
         PLA             ; Esper in slot
         CLC             ; Prepare ADC
         ADC #$36        ; Get attack ID
C35535:  JSR mptext      ; Handle MP cost
         LDA.B #!3dot    ; Char: "…"
         STA $2180       ; Add to string
         LDA $F7         ; Hundreds digit
C3554A:  STA $2180       ; Add to string
C3554D:  JSR add2n       ; Add tens, ones
         JMP C33502      ; Draw string

; Fork: Blank esper slot
C357F0:  LDX.W #!esp_ns+4; Spaces: 12
C357F3:  JSR wm9E8B      ; WRAM: 7E/9E8B
C357F9:  JSR C3C13B      ; Add a space
         DEX             ; One less left
         BNE C357F9      ; Loop till last
         JMP C33502      ; Draw 12 spaces

; Define esper name color
C3557_:  LDA $99         ; Viewed esper
C35574:  PHA             ; Save esper
         JSR C3808A      ; Preset as gray
         LDX #$022B      ; 1st: Guest 2
C3557B:  LDA $161E,X     ; Actor's esper
         CMP $01,S       ; Matches ours?
         BEQ C35597      ; Gray out if so
         REP #$21        ; 16-bit A; C-
         TXA             ; Actor's address
         SBC #$0024      ; Actor slot -1
         TAX             ; Reindex it
         SEP #$20        ; 8-bit A
         BCS C3557B      ; Loop till Terra
         JSR C38085      ; Color: User's
C35597:  TDC             ; Clear A
         PLA             ; Esper to draw
         RTS

; Set source for loading esper name
C354FA:  PEA !esp_ns     ; Letters: 8
         LDY.W #!esp_np  ; Src: E6/F6E1
         LDA.B #!esp_np!h; Source bank
         BRA C3532Y      ; Set bank, size

; Set source for loading Magic name
C34FB5:  PEA !mag_ns     ; Letters: 7
         LDY.W #!mag_np  ; Src: E6/F567
         LDA.B #!mag_np!h; Source bank
         BRA C3532Y      ; Set bank, size

; Set source for loading Lore name
C35266:  PEA !lor_ns     ; Letters: 10
         LDY.W #!lor_np  ; Src: E6/F9FD
         LDA.B #!lor_np!h; Source bank
         BRA C3532Y      ; Set bank, size

; Set source for loading Dance name
C357C1:  PEA !dnc_ns     ; Letters: 12
         LDY.W #!dnc_np  ; Src: E6/FF9D
         LDA.B #!dnc_np!h; Source bank
         BRA C3532Y      ; Set bank, size

; Set source for loading rare item name
C38436:  PEA !rar_ns     ; Letters: 13
         LDY.W #!rar_np  ; Src: CE/FBA0
         LDA.B #!rar_np!h; Source bank
         BRA C3532Y      ; Set bank, size

; Set source for loading enemy name
C35409:  PEA !nme_ns     ; Letters: 10
         LDY.W #!nme_np  ; Src: CF/C050
         LDA.B #!nme_np!h; Source bank
         BRA C3532Y      ; Set bank, size

; Set source for loading SwdTech name
C35328:  PEA !swt_ns     ; Letters: 12
         LDY.W #!swt_np  ; Src: CF/3C40
         LDA.B #!swt_np!h; Source bank
C3532Y:  STA $F1         ; Set src HB
         STY $EF         ; Set src LBs
         PLY             ; String length
         STY $EB         ; Set src size
         RTS

; Set source for loading fixed-size string
C3532Z:  PHX             ; String size
         BRA C3532Y      ; Set data

; Draw "<actor> has it!" in esper data menu
C3559A:  JSR C38085      ; Color: User's
         LDY #$40CD      ; Tilemap ptr
         JSR C3BAB_      ; Set position
         JSR wm9E8B      ; WRAM: 7E/9E8B
         LDY #$0006      ; Letters: 6
C355B2:  LDA $1602,X     ; Name letter
         CMP #$FF        ; Terminator?
         BEQ C355C0      ; Done if so
         STA $2180       ; Add to string
         INX             ; Point to next
         DEY             ; One less left
         BNE C355B2      ; Loop till last
C355C0:  LDX #$FFFF      ; Letter index
C355C2:  INX             ; Letter index +1
         LDA.L C35CD9,X  ; Message letter
         STA $2180       ; Add to string
         BNE C355C2      ; Loop till last
         JMP C38847      ; Draw message

; Draw a row of two Rages
C353EE:  JSR C38085      ; Color: User's
         LDX #$0005      ; X: 5
         JSR C353FD      ; Draw Rage A
         LDX #$0013      ; X: 19
C353FD:  JSR C35409      ; Define source
         JSR C35418      ; Draw Rage B
         BRA C35618      ; Rage slot +1

; Draw a row of two sets of Blitz inputs
C3560A:  LDX #$0004      ; X: 4
         JSR C35615      ; Draw inputs A
         LDX #$0012      ; X: 18
C35615:  JSR sk_slt2     ; Blitz in slot
         BMI C35618      ; Skip if none
         XBA             ; Put it in B
         LDA #$0C        ; Text length
         JSR C350F_      ; X = Blitz x12
         REP #$20        ; 16-bit A
         LDA $7E9E89     ; Tilemap ptr
         STA $2181       ; Set WRAM LBs
         SEP #$20        ; 8-bit A
         TDC             ; Clear A
         LDY #$000A      ; Inputs: 10
C35699:  PHX             ; Save index
         LDA $C47A40,X   ; Blitz input
         ASL A           ; Double it
         TAX             ; Index it
         LDA.L C35C1C,X  ; Tile number
         STA $2180       ; Save in WRAM
         LDA.L C35C1C+1,X; Attributes
         STA $2180       ; Save in WRAM
         PLX             ; Input index
         INX             ; Input index +1
         DEY             ; One less input
         BNE C35699      ; Loop till last
C35618:  INC $E5         ; Blitz slot +1
C3561A:  RTS

; Load Blitz description
C35715:  LDY.W #!btz_d_p ; Ptr loc: CF/FF9E
         LDX.W #!btz_d_l ; Txt loc: CF/FC00
         LDA.B #!btz_d_h ; Bank: CF
C3571Y:  JSR C35C0_      ; Set pointers

; Load skill or item description
C3572A:  JSR C3831A      ; WRAM: 7E/9EC9
C35730:  JSR C3BFC2      ; Skill or item
C35738:  CMP #$FF        ; Empty slot?
         BEQ C3576D      ; Blank if so
         REP #$20        ; 16-bit A
         ASL A           ; Double it
         TAY             ; Index it
         LDA [$E7],Y     ; Relative ptr
         TAY             ; Index it
         SEP #$20        ; 8-bit A
         DEY             ; Index -1
C35745:  INY             ; Index +1
         LDA [$EB],Y     ; Text character
C35749:  STA $2180       ; Add to string
         BNE C35745      ; If not terminator
         RTS

; Fork: Empty slot (space is for item usage menu)
C3576D: ;LDA #$FF        ; Space char
         JMP C380B_      ; Blank string

; Load description for esper data menu
C35B93:  JSR C3831A      ; WRAM: 7E/9EC9
         JSR C35BDB      ; Load pointers
         JSR C3BFC2      ; X = Cursor slot
         LDA $7EAB8D,X   ; Slot content
         BRA C35738      ; Load desc

; Load item description for Sell menu
C3B4F8:  JSR C38308      ; Set desc ptrs
         JSR C3BFCB      ; Item number
         BRA C35738      ; Load desc

; Load item description for relic browsing
C3A1D8:  JSR C38308      ; Set desc ptrs
         JSR invitm_     ; Item number
         BRA C35738      ; Load desc

; 33: Sustain esper data menu
C358CE:  JSR C35983      ; Handle D-Pad
         JSR C35B93      ; Load description
         LDA $08         ; Pushing A?
         BPL C3590A      ; Branch if not
         LDA $4B         ; On esper name?
         BNE C3590A      ; Branch if not
;        LDA $99         ; Viewed esper
         JSR C3557_      ; Choose palette
         LDA $29         ; Esper palette
         CMP #$28        ; Grayed out?
         BNE C35902      ; Equip if not
         JSR C30EC0      ; Play buzzer
         JSR C38590      ; Reset/Stop desc
         JSR C3559A      ; Draw who has it
         LDA #$20        ; Frames: 32
;        STA $20         ; Set exit delay
         JSR C3E3A_      ; Next: Late exit
         JMP C30F11      ; Queue text upload

; Fork: Equip esper
C35902:  JSR C30EB2      ; Sound: Click
         JSR C32929      ; Equip/draw esper
         BRA C35913      ; Exit submenu

; Fork: Handle B
C3590A:  LDA $09         ; Pushing B?
         BPL C3597C      ; Exit if not
         JSR C30EA9      ; Sound: Cursor
C35913:  JSR C38590      ; Reset/Stop desc
         JSR C3546C      ; Draw esper list
         JSR C30EFD      ; Queue list upload
         JSR C31368      ; Refresh esper list
;        TDC             ; BG1 H-Shift: 0
         STA $7E9A11     ; Unhide esper list
         LDY #$0100      ; X: 256
         JSR C3690Y      ; Set for BG2, BG3
         JSR C34E2D      ; Load V-shift data
         JSR C30F11      ; Queue stats upload
         JSR C34B82      ; Load navig data
         JSR oldcur      ; Restore cursor pos
C3597X:  JSR C34C21      ; Relocate cursor
         LDY #$1000      ; V-Speed: 16 px
         JSR C3216_      ; Create scrollbar
         LDA #$32        ; C3/28D3
         STA $26         ; Next: Esper choice
         LDA #$06        ; Top row: Golem's
C3597Y:  LDY #$0208      ; Onscreen rows: 8
C3597Z:  STY $5A         ; Onscreen cols: 2
         STA $5C         ; Set scroll limit
C3597C:  RTS

; 34: Wait while showing who holds esper
C3293A:  LDY $20         ; Timer expired?
         BNE C3597C      ; Exit if not
         LDY #$40CD      ; Tilemap ptr
         JSR C3965_      ; Blank message
         BRA C35913      ; Exit submenu

; Load and draw selected esper's info
C3599F:  JSR C35B54      ; Load esper info
         LDY #$4411      ; Tilemap ptr
         JSR C33519      ; Set pos, WRAM
;        LDA $99         ; Viewed esper
         JSR C3557_      ; Choose palette
         JSR C334F0      ; Draw esper name
         LDY #C35CEA     ; Text pointer
         JSR draw20      ; Draw "Learn.Rate"
         LDY #C35CE2     ; Text pointer
         JSR draw20      ; Draw "Skill"

; Fork: Draw spell data
C359D5:  JSR C3BFC2      ; Spell in slot
         XBA             ; Put it in B
         LDA #$0B        ; Esper data size
         JSR C350F_      ; Esper x size
         LDY #$0011      ; Y: 17
C359EC:  REP #$20        ; 16-bit A
         LDA $D86E00,X   ; Spell, rate
         STA $F5         ; Memorize them
         INX             ; Index +1
         INX             ; Index +1
         PHX             ; Save index
         PHY             ; Save Y position
         PHY             ; Save Y position
         SEP #$20        ; 8-bit A
         LDX #$0018      ; X: 24
         XBA             ; Spell
         JSR C35A84      ; Draw percentage
         LDX #$0005      ; X: 5
         PLY             ; Y position
         LDA $F6         ; Spell
         JSR C35AE1      ; Draw spell, rate
         PLY             ; Y position
         INY             ; Y position +1
         INY             ; Y position +1
         PLX             ; Spell data index
         CPY #$001B      ; Done 5 spells?
         BNE C359EC      ; Loop if not

; Fork: Draw esper bonus message
C35A29:  LDY #$4713      ; Tilemap ptr
         JSR C33519      ; Set pos, WRAM
         LDA $D86E00,X   ; Esper bonus
         BMI C35B26      ; Blank if none
         PHA             ; Save bonus
         LDY #C35CF7     ; Text pointer
         JSR draw20      ; Draw "At level..."
         LDY #$472F      ; Tilemap ptr
         JSR C3BAB_      ; Set position
         LDX.W #!esb_ns  ; Letters: 9
         LDA.B #!esb_np!h; Text ptr HB
         LDY.W #!esb_np  ; Text ptr LBs
         JMP C35EE_      ; Draw bonus

; Draw spell's mastery percentage for esper data menu
C35A84:  JSR C35B3D      ; Set position
         JSR wm9E8B      ; WRAM: 7E/9E8B
         LDA $F6         ; Spell
         JSR C350A2      ; Spell mastery
         BPL C35AAA      ; Branch if < 100%
         LDA #$64        ; Value: 100
C35AAA:  JSR C304E0      ; Turn into text
         LDA $F7         ; Hundreds digit
C35AA6:  JSR add3n_      ; Add tens, ones
C35AB7:  LDA.B #!perc    ; Char: "%"
C35AB9:  STA $2180       ; Add to string
         JMP C33502      ; Draw string

; Draw spell's name and learning rate
C35AE1:  JSR C35B3D      ; Set position
C35AED:  JSR C34FB5      ; Define source
         LDA $F6         ; Spell
         BMI C35B26      ; Blank if none
         JSR C3846_      ; Load name
         LDA.B #!coln    ; Char: ":"
         STA $2180       ; Add to string
         JSR C3C13B      ; Add a space
         STA $2180       ; Add a space
         LDA $F5         ; Learning rate
         JSR C304E0      ; Turn into text
         LDA.B #!mult    ; Char: "×"
         JMP C3554A      ; Complete string

; Fork: Blank spell or bonus line
C35B26:  LDX #$0017      ; Spaces: 23
         JMP C357F3      ; Draw 23 spaces

; Compute position into BG1 tilemap B
C35B3D:  TYA             ; Y position
         JSR C3809F      ; Get BG1a pos
;        CLC             ; Prepare ADC
         ADC #$0800      ; Get BG1b pos
         JMP C3BAB3      ; Set position

; Load esper's info for display
C35B54:  LDX #$AB8D      ; 7E/AB8D
         STX $2181       ; Set WRAM LBs
         LDA $99         ; Chosen esper
         STA $2180       ; Add to list
         XBA             ; Put it in B
         LDA #$0B        ; Esper data size
         JSR C350F_      ; Esper x size
         LDY #$0005      ; Spells left: 5
C35B7B:  INX             ; Skip Learn.Rate
         JSR C35B8A      ; Add spell to list
         INX             ; ROM index +1
         DEY             ; One less spell
         BNE C35B7B      ; Loop till last
C35B8A:  LDA $D86E00,X   ; Level up bonus
         JMP C3C13D      ; Add to list

; Load description pointers for esper data menu
C35BDB:  LDA $4B         ; On esper name?
         BEQ C35C09      ; Branch if so
         CMP #$06        ; On bonus?
         BEQ C35BF6      ; Branch if so

; Fork: Magic effect
C35BE3:  LDY.W #!mag_d_p ; Ptr loc: D8/CF80
         LDX.W #!mag_d_l ; Txt loc: D8/C9A0
         LDA.B #!mag_d_h ; Bank: D8
         BRA C35C0_      ; Branch below

; Fork: Level up bonus
C35BF6:  LDY.W #!esb_d_p ; Ptr loc: ED/FFD0
         LDX.W #!esb_d_l ; Txt loc: ED/FE00
         LDA.B #!esb_d_h ; Bank: ED
         BRA C35C0_      ; Branch below

; Fork: Esper effect
C35C09:  LDY.W #!esp_d_p ; Ptr loc: CF/FE40
         LDX.W #!esp_d_l ; Txt loc: CF/3940
         LDA.B #!esp_d_h ; Bank: CF
C35C0_:  STX $EB         ; Set text loc LBs
         STA $ED         ; Set text loc HB
         JMP C3D51D      ; Set ptr loc

; Draw windows, blue text for non-shifted Status menu
C35D17:  JSR C3352F      ; Reset/Stop stuff
         LDY #C3622A     ; Src: C3/622A
         JSR C3C01B      ; Set to shift text
         JSR C36A15      ; Clear BG1 map A
         JSR bg3d_cl     ; Clear BG3 maps
         JSR mainbox     ; Draw main window
         LDX #C35F79     ; C3/5F79
         JSR C3034Y      ; Draw other boxes
         JSR C35D41      ; LV, HP, MP, %, /
         LDX #C36437     ; Text ptrs loc
         JSR txt2C       ; Color: Blue
         LDA #$15        ; Strings: 21
         JMP C369BA      ; Draw the rest

; Draw LV, HP, MP, and non-blue symbols in Status menu
C35D41:  LDX #C3645B     ; Text ptrs loc
         JSR C39051      ; Draw 2x / and %
         JSR txt24       ; Color: Blue
         LDX #C36455     ; Text ptrs loc
         JMP drawx3      ; Draw "LV/HP/MP"

; Draw Gogo's list of available commands, prepare portrait hiding
C35DC1:  JSR wm9E09      ; WRAM: 7E/9E09
         TDC             ; Clear A

; Fork: Compile everyone's commands, exclude guests and dead Shadow
C35DC8:  LDA #$0F        ; 1st: Guest
C35DC9:  PHA             ; Save char slot
         ASL A           ; Double it
         TAX             ; Index it
         LDY #$0004      ; Commands: 4
         JSR chk1EDX     ; Eligible char?
         BEQ C35DFB      ; Skip if not
         JSR charadY     ; Actor's address
.cmd     LDA $19,X       ; Actor command
         STA $2180       ; Add to list
         DEX             ; Point to next
         DEY             ; One less left
         BNE .cmd        ; Loop till last
         BRA C35E0B      ; Do next actor
C35DFB:  JSR C3C13B      ; Blank command
         DEY             ; One less left
         BNE C35DFB      ; Loop till last
C35E0B:  TDC             ; Clear A
         PLA             ; Actor slot
         DEC A           ; Actor slot -1
         BPL C35DC9      ; Loop till < 0

; Fork: Delete duplicates
C35E12:  LDX #$003F      ; Cmd slot: 64
C35E14:  PHX             ; Save cmd slot
         LDA $7E9E09,X   ; Available cmd
C35E19:  CMP $7E9E08,X   ; Duplicate?
         BNE C35E27      ; Branch if not
         PHA             ; Save command
         LDA #$FF        ; Command: None
         STA $7E9E08,X   ; Clear duplicate
         PLA             ; Battle command
C35E27:  DEX             ; Cmd slot -1
         BNE C35E19      ; Loop till 0
         PLX             ; Base cmd slot
         DEX             ; Cmd slot -1
         BNE C35E14      ; Loop till 0

; Fork: Build final command list
C35E34:  JSR wm9D8A      ; WRAM: 7E/9D8A
         JSR C3C13B      ; Slot 1 = Empty
         TDC             ; Clear A
         TAY             ; Cmd count: 0
         LDX #$003F      ; Cmd slot: 64
C35E42:  PHX             ; Save cmd slot
         LDA $7E9E09,X   ; Available cmd
         BMI C35E60      ; Skip if none
         CMP #$12        ; Mimic?
         BEQ C35E60      ; Skip if so
         ASL A           ; Double it
         TAX             ; Index it
         LDA $CFFE00,X   ; Properties
         LSR A           ; Gogo can use?
         BCC C35E60      ; Skip if not
         TXA             ; Command x2
         LSR A           ; Command
         STA $2180       ; Add to list
         INY             ; Cmd count +1
C35E60:  PLX             ; Cmd slot
         DEX             ; Cmd slot -1
         BPL C35E42      ; Loop till < 0
         INY             ; Cmd count +1
         STY $F7         ; Memorize it
         TYA             ; Put it in A
         JSR C39814      ; Set list size
         XBA             ; Put it in B

; Fork: Load window layout for command list
C35E6D:  LDX #$0007      ; Table index
C35E75:  LDA.L C35F85,X  ; Window data
         JSR C3AA5E      ; Save in RAM
         DEX             ; Index -1
         BPL C35E75      ; Loop till < 0

; Fork: Draw both segments of command window
C35E82:  LDA #$03        ; Multiplier: 3
         JSR mul8        ; Cmd count x3
         LSR A           ; Halve product
         INC A           ; Add 1
         STA $7EAA90     ; Set L-Win heigth
         STA $7EAA94     ; Set R-Win heigth
         LDX #$AA8D      ; 7E/AA8D
         LDA #$7E        ; Bank: 7E
         JSR C30343      ; Draw left box
         LDX #$AA91      ; 7E/AA91
         JSR C30345      ; Draw right box

; Fork: Draw command list
C35EAD:  LDX $00         ; Cmd slot: 1
         LDY #$80C9      ; Tilemap ptr
C35EBA:  PHX             ; Save cmd slot
         PHY             ; Save position
         JSR C33519      ; Set pos, WRAM
         LDA $7E9D8A,X   ; Available cmd
         JSR C35EE4      ; Draw cmd name
         REP #$21        ; 16-bit A; C-
         PLA             ; Text position
         ADC #$0080      ; Go 2 rows down
         TAY             ; Put it in Y
         PLX             ; Cmd slot
         INX             ; Cmd slot +1
         CPX $F7         ; End of list?
         BNE C35EBA      ; Loop if not

; Fork: Set a section to mask wrapping Gogo portrait
C35F50:  LDY #$61CA      ; Tilemap ptr
         JSR C3D51B      ; Set destination
         LDX #$0006      ; Rows left: 6
C35F5C:  LDY $00         ; Map index: 0
         LDA #$3D        ; Pal 7, priority
C35F60:  STA [$E7],Y     ; Set attributes
         INY             ; Index +1
         INY             ; Index +1
         CPY #$0012      ; Done 9 tiles?
         BNE C35F60      ; Loop if not
         LDA $E7         ; Tilemap ptr LB
         ADC #$3F        ; Go 1 row down
         STA $E7         ; Save changes
         BCC C35F75      ; If no overflow
         INC $E8         ; Ptr HB +1
C35F75:  DEX             ; One less row
         BNE C35F5C      ; Loop till last
         RTS

; Select a palette for battle command
C35F25:  CMP #$0B        ; Runic?
         BEQ C35F3A      ; Branch if so
         CMP #$07        ; SwdTech?
         BEQ C35F44      ; Branch if so
C35F2E:  JMP C38085      ; Color: User's

; Fork: Handle Runic
C35F3A:  LDA $11DA       ; R-Hand can Runic?
         ORA $11DB       ; Or L-Hand?
         BMI C35F2E      ; Non-gray if so
C35F34:  JMP txt24       ; Color: Gray

; Fork: Handle SwdTech
C35F44:  LDA $11DA       ; R-Hand flags
         ORA $11DB       ; Add L-Hand's
         BIT #$02        ; Can use SwdTech?
         BEQ C35F34      ; Gray out if not
         BRA C35F2E      ; Use user's color

; Draw actor info in Status menu
C35F8D:  JSR ld28        ; X = Mbr slot
         ASL A           ; Double it
         TAY             ; Y = Slot x2
         LDA $69,X       ; Actor
         LDX $6D,Y       ; Actor's address
         STX $67         ; Memorize it

; Fork: Draw stats at bottom
C35FC2:  JSR C32CE5      ; Load properties
         JSR C399E8      ; Set Bat.Pwr mode
         JSR drwstt2     ; Draw basic stats

; Fork: Draw experience data
C36068:  LDX $67         ; Actor's address
         LDY $11,X       ; Experience LBs
         LDA $13,X       ; Experience HB
         JSR C3058_      ; Turn into text
         LDX #$7CD7      ; Text position
         JSR C304A3      ; Draw 8 digits
         JSR C360A0      ; Get needed exp
         JSR C30582      ; Turn into text
         LDX #$7DD7      ; Text position
         JSR C304A3      ; Draw 8 digits

; Fork: Draw BG1 text
C36049:  LDY #$398F      ; Text position
         JSR C334CF      ; Draw actor name
         LDY #$39B1      ; Text position
         JSR C334E6      ; Draw held esper
         LDX #C36096     ; Coords tbl ptr
         JSR C30C6C      ; Draw LV, HP, MP
         JSR C36102      ; Draw commands

; Fork: Display actor's status
C3608E:  STZ $47         ; Ailments: Off
         JSR C311B0      ; Hide ail. icons
         LDY #$399D      ; Class/KO pos
         LDX #$2050      ; Icon position
C3625_:  TDC             ; Anim mode: 0
C3625B:  STA $48         ; Set for icons
         STX $E7         ; Save icon pos
         JSR C33519      ; Set text pos
         TYX             ; Actor's address
         LDA $14,X       ; Wounded?
         BMI C362E7      ; Draw it if so
         PHA             ; Save status
         AND #$70        ; Clear/Imp/Pet
         STA $E1         ; Memorize them
         PLA             ; Status effects
         AND #$07        ; Dark/Zom/Poison
         ASL A           ; Put in b1-b3
         TSB $E1         ; Add to Imp, etc.
         LDA $15,X       ; Status effects
         AND #$80        ; Isolate Float
         ORA $E1         ; Add Imp, etc.
         BEQ C36306      ; Blank if none
         STZ $F1         ; GFX index LB: 0
         STZ $F2         ; GFX index HB: 0
         LDX #$0007      ; Status left: 7
C3628B:  PHX             ; Save counter
         ASL A           ; Got current?
         BCC C362D5      ; Skip if not
         PHA             ; Save status
         LDY #C37B42     ; C3/7B42
         JSR qanim3      ; Queue icon OAM
         LDA $E8         ; Y position
         JSR C3EA68      ; Follow BG1: Y
         LDA $48         ; Anim mode
         JSR C382D8      ; Set icon's
         PHX             ; Queue index
         LDX $F1         ; GFX index
         REP #$21        ; 16-bit A; C-
         LDA $D8EC25,X   ; Anim table ptr
         PLX             ; Queue index
         JSR setaniY     ; Set icon's ptr
         LDA $E7         ; X position
         JSR C30940      ; Set icon's
         ADC #$0A        ; Add 10 for..
         STA $E7         ; ..next icon
         LDA #$D8        ; Bank: D8
         JSR C3D23A      ; Set ptr HB
         PLA             ; Status effects
C362D5:  INC $F1         ; GFX index +1
         INC $F1         ; GFX index +1
         PLX             ; Status left
         DEX             ; One less left
         BNE C3628B      ; Loop till last

; Fork: Blank "Wounded"
C36306:  JSR C38085      ; Color: User's
C3630_:  LDX #$0007      ; Spaces: 7
         JMP C357F3      ; Draw 7 spaces

; Fork: Draw "Wounded"
C362E7:  LDX $00         ; Index: 0
C362EF:  LDA.L C3371B,X  ; Letter
         STA $2180       ; Add to string
         INX             ; Point to next
         CPX #$0007      ; Copied 7?
         BNE C362EF      ; Loop if not
         JSR C3808A      ; Color: Gray
         JMP C33502      ; Draw string

; Calculate experience needed to level up
C360A0:  TDC             ; Clear A
         TAX             ; Clear X
         STX $F1         ; XP LBs: 0
         STX $F3         ; XP HBs: 0
         LDX $67         ; Actor's address
         LDA $08,X       ; Level
         CMP #$63        ; 99?
         BEQ C360C2      ; Exit if so

; Fork: Calculate total experience needed
C360CA:  TAY             ; Levels left
         TDC             ; Total LBs: 0
         TAX             ; 1st: Level 1
C360D5:  REP #$21        ; 16-bit A; C-
         ADC $ED8220,X   ; Needed XP / 8
         BCC .next       ; If no overflow
         INC $F3         ; Total HB +1
.next    INX             ; Index +1
         INX             ; Index +1
         DEY             ; One less level
         BNE C360D5      ; Loop till last

; Fork: Octuple total to get true value
C360F1:  LDX #$0003      ; Loop counter
.loop    ASL A           ; Total LBs x2
         ROL $F3         ; HBs x2 + C
         DEX             ; Counter -1
         BNE .loop       ; Loop till 0

; Fork: Deduct character's experience
C360AD:  LDX $67         ; Actor's address
         SEC             ; Prepare SBC
         SBC $11,X       ; Deduct XP LBs
         STA $F1         ; Set needed XP
         SEP #$20        ; 8-bit A
         LDA $F3         ; Needed XP HB
         SBC $13,X       ; Deduct XP HB
         STA $F3         ; Save changes
C360C2:  RTS

; Draw actor's command list in Status menu
C36102:  LDY #$7BF1      ; Tilemap ptr
         JSR C34598      ; Draw command 1
         LDY #$7C71      ; Tilemap ptr
         JSR C3459E      ; Draw command 2
         LDY #$7CF1      ; Tilemap ptr
         JSR C345A5      ; Draw command 3
         LDY #$7D71      ; Tilemap ptr
         JMP C345AD      ; Draw command 4

; Handle battle command modifiers
C3612C:  LDA $0016,Y     ; Command
         STA $E0         ; Memorize it
         CMP #$02        ; Magic?
         BNE C3613E      ; Branch if not
         JSR C30D2B      ; Can use magic?
         BCS C3616F      ; Branch if so
         BRA C3616C      ; Command: None

; Fork: Morph
C3613E:  CMP #$03        ; Morph command?
         BNE C3614F      ; Branch if not
         LDA $1DD1       ; Event flags
;        BIT #$04        ; Unlocked Morph?
         BRA C36159      ; Blank if not

; Fork: Leap
C3614F:  CMP #$11        ; Leap command?
         BNE C36160      ; Branch if not
         LDA $11E4       ; Veldt flags
C36159:  BIT #$04        ; Can show Leap?
         BRA C3616A      ; Blank if not

; Fork: Dance
C36160:  CMP #$13        ; Dance command?
         BNE C3616F      ; Branch if not
         LDA $1D4C       ; Known Dances
C3616A:  BNE C3616F      ; Branch if any
C3616C:  LDA #$FF        ; Command: None
         RTS

; Fork: Relic effects
C3616F:  LDX #$0004      ; 1st: Capture
         LDA $11D6       ; Relic effects
         AND #$7C        ; Cmd upgrades
C3617C:  ASL A           ; Got current?
         BPL C36189      ; Skip if not
         PHA             ; Save upgrades
         LDA.L C36198,X  ; Cmd to switch
         CMP $E0         ; Current cmd?
         BEQ C36192      ; Replace if so
         PLA             ; Cmd upgrades
C36189:  DEX             ; Cmd index -1
         BPL C3617C      ; Loop till Jump
         LDA $E0         ; Original cmd
         RTS

; Fork: Replace command
C36192:  PLA             ; Clean stack
         LDA.L C3619D,X  ; New command
         RTS

; Commands to upgrade
C36198:  db $00          ; Fight
         db $02          ; Magic
         db $0D          ; Sketch
         db $0F          ; Slot
         db $05          ; Steal

; Command upgrades
C3619D:  db $16          ; Jump
         db $17          ; X Magic
         db $0E          ; Control
         db $18          ; GP Rain
         db $06          ; Capture

; Create portrait A
C361DA:  JSR C361D_      ; Queue OAM fn
         STA $60         ; Save queue index
C361E5:  RTS

; Queue portrait function
C361D_:  LDY #C30B12     ; C3/0B12
         JSR qanim3      ; Queue OAM fn
         TXA             ; Queue index
         RTS

; 42: Initialize Lineup's Status menu
C3631D:  LDA #$43        ; C3/633F
         JSR C32AA5      ; Queue: Sustain menu
         JSR C35D17      ; Draw boxes, etc.
         LDA $C9         ; Actor
         JSR C35FC2      ; Draw actor info
         JSR C3319F      ; Upload tilemaps
         JSR C361DA      ; Init portrait
         JSR db7e_       ; Set DB to 7E
         LDA $C9         ; Actor
         TAY             ; Index it
         LDA $1850,Y     ; Actor's row, etc.
         JSR C30AFB      ; Set portrait's X
         LDY $00         ; Portrait slot: 1
         JMP C361FB      ; Set Y/ptr/timer

; 43: Sustain Lineup's Status menu
C3633F:  LDA $09         ; Pushing B?
         BPL C361E5      ; Exit if not
         LDA #$04        ; Menu: Lineup
         STA $0200       ; Restore root menu
         LDA #$44        ; C3/7131
         JMP C3224F      ; Sound + Fade-out

; 1F: Sustain shifted Status menu
C363A7:  JSR C36409      ; Handle D-Pad
         LDA $08         ; Pushing A?
         BPL C363DA      ; Branch if not
         JSR C30EB2      ; Sound: Click
         JSR C34EDD      ; Actor's address
         LDA $5E         ; Edited slot
.loop    INY             ; Address +1
         DEC A           ; Slot -1
         BPL .loop       ; Loop till < 0
         JSR invitm_     ; Chosen command
         TXA             ; Put in A
         STA $0015,Y     ; Assign to actor
         JSR C36102      ; Redraw commands
         BRA C363E3      ; Shift screen

; Fork: Handle B
C363DA:  LDA $09         ; Pushing B?
         BPL C36402      ; Exit if not
         JSR C30EA9      ; Sound: Cursor
C363E3:  DEC $46         ; Blinker: Off
         JSR C315B8      ; Load navig data
         LDA $5E         ; Old cursor row
         STA $4E         ; Set as current
         JSR C33708      ; Relocate cursor
         LDY #$0C65      ; Next: Shift screen
C363FX:  LDA #$06        ; Frames: 6
C363FY:  STA $20         ; Set scroll timer
         STY $26         ; Queue: Normal menu
         LDY #$FFF4      ; Speed: -12
         STY $9C         ; Set scroll speed
C36402:  RTS

; 5D: Initialize actor naming menu
C3652D:  JSR C3352X      ; Reset/Stop stuff
         LDX $0201       ; Actor's address
         STX $67         ; Memorize it
         JSR C36771      ; Upload portrait GFX
         LDY #C36647     ; C3/6647
         JSR C305FE      ; Load navig data
         JSR C36641      ; Relocate cursor
         JSR bg1c_cl     ; Clear BG1 maps
         JSR C36A28      ; Clear BG2 map A
         JSR C36A3C      ; Clear BG3 map A
         LDX #C36761     ; C3/6761
         JSR C3034Y      ; Draw windows
         JSR C38085      ; Color: User's
         JSR C367BF      ; Draw options
         JSR C3675B      ; Draw actor name
         LDY #C368E3     ; Text pointer
         JSR draw20      ; Draw "Please..."
         JSR mvmapx4     ; Upload tilemaps
         LDX $67         ; Actor's address
         LDY $00         ; Column: 1st
C36625:  STY $28         ; Set arrow column
         LDA $02,X       ; Current letter
         INC A           ; Terminator?
         BEQ C36634      ; Branch if so
         INX             ; Point to next
         INY             ; Arrow column +1
         CPY #$0006      ; Done 6 letters?
         BNE C36625      ; Loop if not
C36634:  JSR C3683C      ; Set to shift text
         JSR C3678C      ; Get portrait colors
         JSR C366D8      ; Create portrait
         LDY #C368A3     ; C3/68A3
         JSR qanim2      ; Queue arrow OAM
         LDA #$5F        ; Queue: Sustain menu
         JMP C346A_      ; Queue cursor OAM

; 5F: Sustain actor naming menu
C3656C:  JSR C30EE9      ; Queue text upload
         JSR C3663E      ; Handle D-Pad
         JSR C3675B      ; Redraw actor name

; Fork: Handle Start
C36575:  LDA $09         ; No-autofire keys
         BIT #$10        ; Pushing Start?
         BNE C365C2      ; Branch if so

; Fork: Point to current letter
C3657_:  LDY $67         ; Actor's address
         LDA $28         ; Arrow column
.loop    INY             ; Address +1
         DEC A           ; Column -1
         BPL .loop       ; Loop till < 0
         TYX             ; Copy to X

; Fork: Handle A
C3657E:  LDA $08         ; Pushing A?
         BPL C36595      ; Branch if not
         JSR C30EB2      ; Sound: Click
         LDA $28         ; Arrow's column
         CMP #$05        ; Rightmost?
         BEQ C36594      ; Branch if so
         INC $28         ; Go right by 1
C36594:  JSR C3BFC2      ; X = Cursor slot
         LDA.L !anm_ct,X ; Chosen letter
         TYX             ; RAM index
         BRA C3659_      ; Add letter

; Fork: Handle B
C36595:  LDA $09         ; Pushing B?
         BPL C365C1      ; Exit if not
         JSR C30EC7      ; Sound: Delete
         LDA $28         ; Arrow's column
         BEQ C365C1      ; Exit if first
         CMP #$05        ; Rightmost?
         BNE C365B5      ; Branch if not
         LDA #$FF        ; Char: Terminator
         CMP $01,X       ; Letter 6's blank?
         BEQ C365B5      ; Branch if so
C3659_:  STA $01,X       ; Clear letter 6
         RTS

; Fork: Delete non-sixth letter
C365B5:  LDA #$FF        ; Char: Terminator
         STA $00,X       ; Clear last letter
         DEC $28         ; Move arrow left
C365C1:  RTS

; Fork: Validate entered actor name
C365C2:  LDX $67         ; Actor's address
         LDA $02,X       ; First letter
         INC A           ; Blank letter?
         BNE C365DB      ; Valid if not
         JMP C3B66F      ; Buzzer + Mosaic
C365DB:  JMP C3AA9_      ; Sound + Fade-out

; Draw actor name for actor naming menu
C3675B:  LDY #$4229      ; Text position
         JMP C334CF      ; Draw actor name

; Upload portrait for actor naming menu
C36771:  JSR C36EBZ      ; Set source
         LDX #$2600      ; $2600
         STX $1B         ; Set VRAM ptr
C3677_:  JSR C314AC      ; Upload portrait
         STZ $1C         ; Dequeue DMA
         RTS

; Load portrait colors for actor naming menu
C3678C:  LDX $67         ; Actor's address
         TDC             ; Clear A
         LDA $01,X       ; Graphics set
C3678X:  LDY #$3149      ; Dst: 7E/3149
C3678Y:  TAX             ; Index GFX set
         LDA.L C36F00,X  ; Palette number
         JSR x32r        ; x32
         ADC #$5860      ; Add base adr
         TAX             ; Set src LBs
         SEP #$20        ; 8-bit A
         LDA #$ED        ; Src HB: ED
         JMP C30F8E      ; Load colors

; Draw options in actor naming menu
C367BF:  LDY.W #!anm_ct  ; Src: D8/E8C8
         LDX #$399B      ; Dst: 7E/399B
         LDA #$7E        ; Dst bank: 7E
         JSR C35C0_      ; Set pointers
         LDA.B #!anm_ct!h; Bank: D8
         STA $E9         ; Set src HB
         LDY $00         ; RAM index: 0
         LDA #$07        ; Rows left: 7
C367B_:  PHA             ; Save counter
C367C7:  LDA #$0A        ; Columns: 10
C367C_:  PHA             ; Save counter

; Fork: Add character to tilemap
C367CD:  LDA $29         ; Palette, etc.
         XBA             ; Put in B
         LDA [$E7]       ; Font character
         REP #$20        ; 16-bit A
         STA [$EB],Y     ; Set attributes
         INC $E7         ; ROM address +1
         SEP #$20        ; 8-bit A
         INY             ; RAM index +1
         INY             ; RAM index +1
         INY             ; RAM index +1
         INY             ; RAM index +1
         PLA             ; Columns left
         CMP #$06        ; At separator?
         BNE C36827      ; Branch if not
         INY             ; RAM index +1
         INY             ; RAM index +1
C36827:  DEC A           ; One less column
         BNE C367C_      ; Loop till last
         REP #$21        ; 16-bit A; C-
         TYA             ; RAM index
         ADC #$0056      ; Go 2 rows down
         TAY             ; Reindex it
         SEP #$20        ; 8-bit A
         PLA             ; Rows left
         DEC A           ; One less left
         BNE C367B_      ; Loop till last
         RTS

; Set to condense text in actor naming menu
C3683C:  LDY #C36896     ; Src: C3/6896
         JSR C3949A      ; Queue BG1 V-Pan
         LDY #C36889     ; C3/6889
         LDA #$C3        ; Bank: C3
dma6a:   STA $4364       ; Set CH6 src HB
         LDX #$0D02      ; 1Rx2B to $210D
dma6b:   STX $4360       ; Set CH6 dst/mode
         STY $4362       ; Set CH6 src LBs
         LDA #$40        ; Channel: 6
         TSB $43         ; Queue HDMA-6
         RTS

; Update arrow in actor naming menu
C368A3:  BNE C368CB      ; If mode 1

; Mode 0: Initialize arrow
C368AB:  LDA #$40        ; Y: 64
         STA $344A,X     ; Set sprite's
         LDY #C3B452     ; Anim table ptr
         JSR initob1     ; Set pose timer

; Mode 1: Sustain arrow
C368CB:  LDA $28         ; Arrow's slot
         ASL A           ; x2
         ASL A           ; x4
         ASL A           ; x8
         ADC #$78        ; Add base Y-Pos
         STA $33CA,X     ; Set X position
         JMP C30860      ; Set OAM; requeue

; Reset menu or ending variables
C368FA:  JSL $D4CDF3     ; Reset many vars
         JSR C36904      ; Reset BGs' X/Y

; Fork: Clear buffer for VWF description graphics
C3A796:  LDY #$0700      ; Bytes: 1792
C3A79X:  LDX #$A271      ; To 7E/A271
C3A79_:  PHB             ; Save DB
         TDC             ; Zero A
         PHA             ; Put on stack
         PLB             ; Set DB to 0
         JSR C300C_      ; WRAM bank: 7E
         JSR clrvwf      ; Handle DMA
         PLB             ; Restore DB
         RTS

; Handle DMA transfer for the above
clrvwf:  PEA $8008       ; WRAM/fixed/1x1
         STX $2181       ; Set WRAM LBs
         LDX #blkobj     ; Src: C3/B45B
         LDA #$C3        ; Src bank
         JMP dmast2      ; Start DMA

; Get party info from SRAM block
C36915:  JSL $D4CA00     ; Clear all info
         LDY #$000F      ; 1st: Guest 2
C3691C:  TDC             ; Clear A
         LDA $1850,Y     ; Party info
         TAX             ; Copy to X
         BIT #$40        ; Object's off?
         BEQ C36941      ; Skip if so
         AND #$07        ; Get party
         CMP $1A6D       ; Active party?
         BNE C36941      ; Skip if not
         PHY             ; Save slot
         TXA             ; Party info
         PHA             ; Memorize it
         AND #$18        ; Roster slot
         LSR A           ; Put in b2-b3
         LSR A           ; Put in b1-b2
         LSR A           ; Now 0-3
         TAX             ; Index it
         PLA             ; Party info
         STA $75,X       ; Set member's
         TYA             ; Actor slot
         STA $69,X       ; Set member's
         PHA             ; Save slot
         TXA             ; Member slot
         ASL A           ; Double it
         TAY             ; Index it
         PLA             ; Actor slot
         JSR charadr     ; Actor's address
         STX $6D,Y       ; Save address
         PLY             ; Actor slot
C36941:  DEY             ; Actor slot -1
         BPL C3691C      ; Loop till < 0
         RTS

; SRAM block addresses for actor data
C36969:  dw $1600        ; 00: Terra
         dw $1625        ; 01: Locke
         dw $164A        ; 02: Cyan
         dw $166F        ; 03: Shadow
         dw $1694        ; 04: Edgar
         dw $16B9        ; 05: Sabin
         dw $16DE        ; 06: Celes
         dw $1703        ; 07: Strago
         dw $1728        ; 08: Relm
         dw $174D        ; 09: Setzer
         dw $1772        ; 0A: Mog
         dw $1797        ; 0B: Gau
         dw $17BC        ; 0C: Gogo
         dw $17E1        ; 0D: Umaro
         dw $1806        ; 0E: Guest 1
         dw $182B        ; 0F: Guest 2

; Set Window 1 positions per scanline
C369DC:  LDA #$01        ; 2Rx1B to PPU
         STA $4320       ; Set CH2 mode
         LDA #$26        ; To $2126, $2127
         LDY #C369FB     ; Src: C3/69FB
         JMP dma2        ; Queue HDMA-2

; Window 1 positions per scanline
C369FB:  db $07,$FF,$00  ; No range
         db $78,$08,$F7  ; 8, 247
         db $58,$08,$F7  ; 8, 247
         db $08,$FF,$00  ; No range
         db $00          ; End

; Set global Window 1 positions
C36A08:  LDA #$04        ; Channel: 2
         TRB $43         ; Halt Win1 HDMA
         STZ $2126       ; Win1-L: 0
         LDA #$FF        ; Win1-R: 255
         STA $2127       ; Set register
         RTS

; Clear BG1 tilemap A
C36A15:  LDX #$3849      ; 7E/3849
         BRA C36A4E      ; Clear map

; Clear BG1 tilemap B
C36A19:  LDX #$4049      ; 7E/4049
         BRA C36A4E      ; Clear map

; Clear BG1 tilemap C
C36A1E:  LDX #$4849      ; 7E/4849
         BRA C36A4E      ; Clear map

; Clear BG1 tilemap D
C36A23:  LDX #$5049      ; 7E/5049
         BRA C36A4E      ; Clear map

; Clear BG2 tilemap A
C36A28:  LDX #$5849      ; 7E/5849
         BRA C36A4E      ; Clear map

; Clear BG2 tilemap B
C36A2D:  LDX #$6049      ; 7E/6049
         BRA C36A4E      ; Clear map

; Clear BG2 tilemap C
C36A32:  LDX #$6849      ; 7E/6849
         BRA C36A4E      ; Clear map

; Clear BG2 tilemap D (unused)
;C36A37: LDX #$7049      ; 7E/7049
;        BRA C36A4E      ; Clear map

; Clear BG3 tilemap A
C36A3C:  LDX #$7849      ; 7E/7849
         BRA C36A4E      ; Clear map

; Clear BG3 tilemap B
C36A41:  LDX #$8049      ; 7E/8049
         BRA C36A4E      ; Clear map

; Clear BG3 tilemap C
C36A46:  LDX #$8849      ; 7E/8849
         BRA C36A4E      ; Clear map

; Clear BG3 tilemap D
C36A4B:  LDX #$9049      ; 7E/9049
C36A4E:  LDY #$0800      ; Bytes: 2048
         JMP C3A79_      ; Clear map

; Load non-skin graphics and colors for menu
C36A87:  LDA $0200       ; Menu
         CMP #$07        ; Final battle?
         BEQ C36AB0      ; Branch if so
         DEC A           ; Actor naming?
         BEQ C36B04      ; Branch if so
         DEC A           ; Adjust value
         DEC A           ; Shop menu?
         BEQ C36ACE      ; Branch if so
         DEC A           ; Lineup menu?
         BEQ C36AB0      ; Branch if so

; Fork: Load, Main, or Colosseum menu
C36AE9:  JSR C36E82      ; Load portraits
         JSR C36CE9      ; Actor sprites
         BRA C36B04      ; Branch below

; Fork: Lineup or final battle menu
C36AB0:  JSR C37010      ; Blank portrait
         JSR C36CE9      ; Actor sprites
         JSR C36CC7      ; Actor colors
         BRA C36B04      ; Branch below

; Fork: Shop menu
C36ACE:  JSR C36D79      ; Actor sprites
         JSR C36CC7      ; Actor colors

; Fork: Actor naming menu
C36B04:  JSR C36BE8      ; Font colors

; Fork: Upload BG3 font graphics, erase VWF description
C36B13:  LDY #$6000      ; $6000
         STY $2116       ; Set VRAM ptr
         LDX #$7FC0      ; Src: C4/7FC0
         LDA #$C4        ; Src bank
         LDY #$1000      ; Tiles: 256
         JSR dmast1      ; Upload font
         LDY #$0400      ; Tiles: 128
         JSR C3DD7A      ; Clear desc

; Fork: Upload BG1 font graphics
C36B37:  LDY #$5000      ; $5000
         STY $2116       ; Set VRAM ptr
         REP #$20        ; 16-bit A
         LDX $00         ; Index: 0
C36B41:  LDY #$0008      ; 8-px rows: 8
C36B44:  LDA.L C36B9C,X  ; Square GFX
         STA $2118       ; Save in VRAM
         DEY             ; One less row
         BNE C36B44      ; Loop till last
         INX             ; Index +1
         INX             ; Index +1
         CPX #$0020      ; Done 8 tiles?
         BNE C36B41      ; Loop if not
         SEP #$20        ; 8-bit A
         LDY #$8040      ; C4/8040
         LDA #$C4        ; Bank: C4
         JSR C3D51D      ; Set source
         LDX #$00F8      ; Tiles: 248
         JSR C3DD8D      ; Upload font

; Fork: Load cursor, arrow, and icon colors
C36C84:  LDA #$EC        ; Object color 108
         STA $2121       ; CGRAM: $01D8
         LDA #$08        ; Colors: 4
         LDX #$E8C0      ; Src: D8/E8C0
         LDY #$3221      ; Dst: 7E/3221
         JSR C36BEE      ; Status icon pals
         LDA #$08        ; Colors: 4
         LDX #$E8B8      ; Src: D8/E8B8
         LDY #$3241      ; Dst: 7E/3241
         JSR C36BEE      ; Cursor, etc. pals

; Fork: Upload cursor, arrow, and icon graphics (plus excess data)
C36E67:  LDY #$2000      ; $2000
         STY $2116       ; Set VRAM ptr
         LDX #$5AC0      ; Src: ED/5AC0
         LDA #$ED        ; Source bank
         LDY #$1400      ; Bytes: 5120
         JMP dmast1      ; Move data

; Square BG1 tiles, encoded as a single row
C36B9C:  dw $0000,$0000  ; Blank tile
         dw $00FF,$0000  ; Config: Palette color 1
         dw $FF00,$0000  ; Config: Palette color 2
         dw $FFFF,$0000  ; Config: Palette color 3
         dw $0000,$00FF  ; Config: Palette color 4
         dw $00FF,$00FF  ; Config: Palette color 5
         dw $FF00,$00FF  ; Config: Palette color 6
         dw $FFFF,$00FF  ; Config: Palette color 7

; Load font and black background's colors
C36BE8:  STZ $2121       ; To BG color 0
         LDA #$A0        ; Colors: 80
         LDX #$E800      ; Src: D8/E800
         LDY #$3049      ; Dst: 7E/3049
C36BEE:  STY $2181       ; Set WRAM LBs
.loop    PHA             ; Save counter
         LDA $D80000,X   ; BG color data
         STA $2180       ; Save in RAM
         STA $2122       ; Save in CGRAM
         INX             ; Src index +1
         PLA             ; Bytes left
         DEC A           ; One less left
         BNE .loop       ; Loop till last
         RTS

; Load actor sprite palettes
C36CC7:  LDX $00         ; Color index: 0
         LDA #$A0        ; Object color 32
         STA $2121       ; CGRAM: $0140
C36CCE:  LDA $ED6300,X   ; Color data
         STA $7E3189,X   ; Save in RAM
         STA $2122       ; Save in CGRAM
         INX             ; Index +1
         CPX #$00C0      ; Done 96 colors?
         BNE C36CCE      ; Loop if not
         RTS

; Upload actor sprite graphics (one pose)
C36CE9:  LDX #$002A      ; Sprite index
C36CEB:  PHX             ; Save index
         REP #$20        ; 16-bit A
         LDA $CFF8E5,X   ; VRAM pointer
         PHA             ; Memorize it
         STA $2116       ; Set destination
         TXA             ; Sprite index
         ASL A           ; Double it
         TAX             ; Index it
         LDA $CFF913,X   ; GFX adr LBs
         STA $E7         ; Memorize it
         LDA $CFF911,X   ; GFX adr HBs
         STA $E9         ; Memorize it
         LDX $00         ; Ptr index: 0

; Fork: Upload a row of two tiles
C36D05:  LDA $CFF8DF,X   ; Tile rel ptr
         CLC             ; Prepare ADC
         ADC $E7         ; Add base adr LBs
         STA $EB         ; Set src LBs
         TDC             ; Zero A
         ADC $E9         ; Add adr HBs + C
         STA $ED         ; Set source HBs
         LDY $00         ; Index: 0
C36D5A:  LDA [$EB],Y     ; Tile graphics
         STA $2118       ; Save in VRAM
         INY             ; Index +1
         INY             ; Index +1
         CPY #$0040      ; Done 2 tiles?
         BNE C36D5A      ; Loop if not

; Fork: Handle next row
C36D0E:  PLA             ; VRAM pointer
         CLC             ; Prepare ADC
         ADC.L v_act,X   ; Add modifier
         PHA             ; Memorize it
         STA $2116       ; Set destination
         INX             ; Ptr index +1
         INX             ; Ptr index +1
         CPX #$0006      ; Done 3 rows?
         BNE C36D05      ; Loop if not
         PLA             ; Clean stack

; Fork: Blank both tiles under feet
C36D67:  LDY #$0020      ; Tiles: 2
         JSR C3DD7A      ; Make tiles

; Fork: Handle next sprite
C36D39:  PLX             ; Sprite index
         DEX             ; Index -1
         DEX             ; Index -1
         BPL C36CEB      ; Loop till < 0
         SEP #$20        ; 8-bit A
         RTS

; VRAM pointer modifiers for actor parts
v_act:   dw $0100        ; Torso
         dw $FF20        ; Legs
         dw $0100        ; Blank row

; Upload actor sprites for shop
C36D79:  LDY #$3000      ; $3000
         STY $2116       ; Set VRAM ptr
         TDC             ; Actor duo: 1

; Fork: Load both poses for two actors
C36D81:  PHA             ; Save actor duo
         LDX $00         ; Tile slot: 1
C36D83:  PHX             ; Save tile slot
         LDY #$2D40      ; GFX data size x2
         LDA $03,S       ; Actor duo
         PHX             ; Save X
         JSR mul16       ; Actor duo x size
         PLX             ; Restore X
         TDC             ; Clear A
         LDA.L C36E07,X  ; GFX ptr index
         TAX             ; Index it
         REP #$21        ; 16-bit A; C-
         LDA.L C36E27,X  ; Tile's GFX ptr
         BMI C36DEA      ; If blank tile
         ADC $2134       ; Add product LBs
         STA $EB         ; Set source LBs
         SEP #$20        ; 8-bit A
         LDA $2136       ; Product HB
         ADC #$D5        ; Add base bank
         STA $ED         ; Set source HB
         REP #$20        ; 16-bit A
         LDY $00         ; Index: 0
C36DFA:  LDA [$EB],Y     ; 8 px, 2 planes
         STA $2118       ; Save in VRAM
         INY             ; Index +1
         INY             ; Index +1
         CPY #$0020      ; Done 8 rows?
         BNE C36DFA      ; Loop if not

; Fork: Handle next tile
C36DD7:  SEP #$20        ; 8-bit A
         PLX             ; Tile slot
         INX             ; Tile slot +1
         CPX #$0020      ; Done 32 tiles?
         BNE C36D83      ; Loop if not

; Fork: Handle next two actors
C36DDD:  PLA             ; Actor duo
         INC A           ; Actor duo +1
         CMP #$08        ; Done 16 chars?
         BNE C36D81      ; Loop if not
         RTS

; Fork: Create a blank tile
C36DEA:  JSR C3B10A      ; Make blank tile
         BRA C36DD7      ; Do next tile

; Indices for loading tiles from two shop actors
C36E07:  db $00          ; Actor1 X1,Y1 (hands down)
         db $02          ; Actor1 X1,Y2 (hands down)
         db $08          ; Actor1 X3,Y1 (hands down)
         db $0A          ; Actor1 X3,Y2 (hands down)
         db $10          ; Actor1 X1,Y1 (hands up)
         db $12          ; Actor1 X1,Y2 (hands up)
         db $18          ; Actor1 X3,Y1 (hands up)
         db $1A          ; Actor1 X3,Y2 (hands up)
         db $20          ; Actor2 X1,Y1 (hands down)
         db $22          ; Actor2 X1,Y2 (hands down)
         db $28          ; Actor2 X3,Y1 (hands down)
         db $2A          ; Actor2 X3,Y2 (hands down)
         db $30          ; Actor2 X1,Y1 (hands up)
         db $32          ; Actor2 X1,Y2 (hands up)
         db $38          ; Actor2 X3,Y1 (hands up)
         db $3A          ; Actor2 X3,Y2 (hands up)
         db $04          ; Actor1 X2,Y1 (hands down)
         db $06          ; Actor1 X2,Y2 (hands down)
         db $0C          ; Actor1 X4,Y1 (hands down)
         db $0E          ; Actor1 X4,Y2 (hands down)
         db $14          ; Actor1 X2,Y1 (hands up)
         db $16          ; Actor1 X2,Y2 (hands up)
         db $1C          ; Actor1 X4,Y1 (hands up)
         db $1E          ; Actor1 X4,Y2 (hands up)
         db $24          ; Actor2 X2,Y1 (hands down)
         db $26          ; Actor2 X2,Y2 (hands down)
         db $2C          ; Actor2 X4,Y1 (hands down)
         db $2E          ; Actor2 X4,Y2 (hands down)
         db $34          ; Actor2 X2,Y1 (hands up)
         db $36          ; Actor2 X2,Y2 (hands up)
         db $3C          ; Actor2 X4,Y1 (hands up)
         db $3E          ; Actor2 X4,Y2 (hands up)

; Tile graphics pointers for odd-numbered shop actors
C36E27:  dw $03C0        ; 1,1 (hands down)
         dw $03E0        ; 1,2 (hands down)
         dw $0500        ; 2,1 (hands down)
         dw $0520        ; 2,2 (hands down)
         dw $0540        ; 3,1 (hands down)
         dw $0560        ; 3,2 (hands down)
         dw $FFFF        ; 4,1 (hands down)
         dw $FFFF        ; 4,2 (hands down)
         dw $03C0        ; 1,1 (hands up)
         dw $0660        ; 1,2 (hands up)
         dw $0680        ; 2,1 (hands up)
         dw $06A0        ; 2,2 (hands up)
         dw $06C0        ; 3,1 (hands up)
         dw $06E0        ; 3,2 (hands up)
         dw $FFFF        ; 4,1 (hands up)
         dw $FFFF        ; 4,2 (hands up)

; Tile graphics pointers for even-numbered shop actors
         dw $1A60        ; 1,1 (hands down)
         dw $1A80        ; 1,2 (hands down)
         dw $1BA0        ; 2,1 (hands down)
         dw $1BC0        ; 2,2 (hands down)
         dw $1BE0        ; 3,1 (hands down)
         dw $1C00        ; 3,2 (hands down)
         dw $FFFF        ; 4,1 (hands down)
         dw $FFFF        ; 4,2 (hands down)
         dw $1A60        ; 1,1 (hands up)
         dw $1D00        ; 1,2 (hands up)
         dw $1D20        ; 2,1 (hands up)
         dw $1D40        ; 2,2 (hands up)
         dw $1D60        ; 3,1 (hands up)
         dw $1D80        ; 3,2 (hands up)
         dw $FFFF        ; 4,1 (hands up)
         dw $FFFF        ; 4,2 (hands up)

; Upload each member's portrait
C36E82:  LDX #$0006      ; Member slot: 4
C36E84:  PHX             ; Save mbr index
         JSR C36EBW      ; Set src, dst
         JSR C3677_      ; Upload portrait
         PLX             ; Member index
         DEX             ; Member index -1
         DEX             ; Member index -1
         BPL C36E84      ; Loop till < 0

; Fork: Load portrait colors for entire party
C36C09:  LDY #$0006      ; Member index: 0
         LDX #$31A9      ; Dst: 7E/31A9
C36C0C:  PHY             ; Save member index
         PHX             ; Save destination
         PHX             ; Save destination
         LDX $6D,Y       ; Actor data index
         JSR gfxset2     ; Portrait number
         BEQ C36C30      ; Branch if Imp's
C36C25:  LDA $00,X       ; Actor
         CMP #$01        ; Locke?
         BEQ C36C30      ; Branch if so
         LDA $01,X       ; Graphics set
C36C30:  PLY             ; RAM index
         JSR C3678Y      ; Load palette
         PLX             ; RAM index
         LDA #$20        ; Pal data size
.loop    DEX             ; RAM index -1
         DEC A           ; Data size -1
         BNE .loop       ; Loop till 0
         PLY             ; Member index
         DEY             ; Member index -1
         DEY             ; Member index -1
         BPL C36C0C      ; Loop till < 0
         RTS

; Portrait palette assignments
C36F00:  db $00          ; 00: Terra
         db $01          ; 01: Locke
         db $02          ; 02: Cyan
         db $03          ; 03: Shadow
         db $04          ; 04: Edgar
         db $05          ; 05: Sabin
         db $06          ; 06: Celes
         db $07          ; 07: Strago
         db $08          ; 08: Relm
         db $09          ; 09: Setzer
         db $0A          ; 0A: Moogle
         db $0B          ; 0B: Gau
         db $0C          ; 0C: Gogo
         db $0D          ; 0D: Umaro
         db $0E          ; 0E: Soldier
         db $0F          ; 0F: Imp
         db $10          ; 10: Leo
         db $11          ; 11: Banon
         db $00          ; 12: Terra (unused; esper Terra)
         db $0E          ; 13: Soldier (unused; merchant)
         db $12          ; 14: Ghost

; Pointers to portrait graphics
C36F1B:  dw $0000        ; 00: Terra
         dw $0320        ; 01: Locke
         dw $0640        ; 02: Cyan
         dw $0960        ; 03: Shadow
         dw $0C80        ; 04: Edgar
         dw $0FA0        ; 05: Sabin
         dw $12C0        ; 06: Celes
         dw $15E0        ; 07: Strago
         dw $1900        ; 08: Relm
         dw $1C20        ; 09: Setzer
         dw $1F40        ; 0A: Mog
         dw $2260        ; 0B: Gau
         dw $2580        ; 0C: Gogo
         dw $28A0        ; 0D: Umaro
         dw $2BC0        ; 0E: Soldier
         dw $2EE0        ; 0F: Imp
         dw $3200        ; 10: Leo
         dw $3520        ; 11: Banon
         dw $0000        ; 12: Terra (unused; esper Terra)
         dw $2BC0        ; 13: Soldier (unused; merchant)
         dw $3840        ; 14: Ghost

; VRAM pointers for portrait graphics
C36F51:  dw $2600        ; Member A
         dw $2800        ; Member B
         dw $2A00        ; Member C
         dw $2C00        ; Member D

; Create a blank portrait for Lineup menu
C37010:  LDX #$9F51      ; 7E/9F51
         LDY #$0320      ; Bytes: 800
         JMP C3A79_      ; Clear data

; Get Square's marker, point to last save file
srm_mrk: LDX #$0004      ; File slot: 3
         REP #$20        ; 16-bit A
         LDA #$E41B      ; Square's marker
         RTS

; Reset saved cursor positions for main menu
C37068:  LDX #$001E      ; Position index
C3706B:  STZ $022B,X     ; Clear position
         DEX             ; Index -1
         BPL C3706B      ; Loop till < 0
         LDX #$0006      ; Member slot: 4
.loop    INC $0237,X     ; Skills: "Magic"
         DEX             ; Member index -1
         DEX             ; Member index -1
         BPL .loop       ; Loop till < 0
         RTS

; Reset game data for Load menu
C3709B:  JSR C37068      ; Undo cursor mem
         LDY #$7FFF      ; Color: White
         STY $1D55       ; Set user font's
         LDA #$2A        ; Bat.Mode, etc.
         STA $1D4D       ; Define options
         TDC             ; Clear A
         TAY             ; Clear Y
         STZ $1D54       ; Mag.Order, etc.
         STZ $1D4E       ; Wallpaper, etc.
         STZ $1D4F       ; Pad assignments
         STY $1863       ; SRAM hours, mins
         STZ $1865       ; SRAM seconds
         STY $1860       ; Gold LBs
         STZ $1862       ; Gold HB
         STY $1866       ; Steps LBs
         STZ $1868       ; Steps HB

; Fork: Reset all skin colors
C36BBC: ;LDY $00         ; RAM index: 0
         TYX             ; ROM index: 0
         LDA #$07        ; Skins left: 8
C36BC7:  PHA             ; Save counter
         LDA #$0E        ; Colors left: 7
C36BCC:  PHA             ; Save counter
         LDA $ED1C02,X   ; Default color
         STA $1D57,Y     ; Save in options
         INX             ; ROM index +1
         INY             ; RAM index +1
         PLA             ; Bytes left
         DEC A           ; One less left
         BNE C36BCC      ; Loop till last
         LDA #$12        ; Loop counter
.loop    INX             ; ROM index +1
         DEC A           ; Counter -1
         BNE .loop       ; Loop till 0
         PLA             ; Skins left
         DEC A           ; One less skin
         BPL C36BC7      ; Loop till last
         RTS

; 69: Initialize Lineup menu
C370E7:  JSR C370FC      ; Init variables
         JSR C3762A      ; List/Create actors
         JSR C37482      ; Load navig data
         JSR C3748B      ; Relocate cursor
C37114:  JSR C307B0      ; Queue cursor OAM
         LDY #C375E4     ; Src: C3/75E4
         JSR C3949A      ; Set to shift text
         JSR C36A15      ; Clear BG1 map A
         JSR C36A3C      ; Clear BG3 map A
         LDX #C37598     ; C3/7598
         JSR C3034Y      ; Draw main boxes
         JSR ptycnt      ; One group?
         BEQ .skip       ; Branch if so
         LDX #C375B4     ; C3/75B4
         DEC A           ; Two groups?
         BEQ .draw       ; Branch if so
         LDX #C375B0     ; C3/75B0
.draw    JSR C3034Y      ; Draw box(es)
.skip    LDY #C37AAE     ; Text pointer
         JSR draw24      ; Draw "Lineup"
         JSR C37562      ; Draw "Form..."
         JSR mvmapx3     ; Upload tilemaps
         JSR C3793F      ; Create portrait
         JSR C37953      ; Draw actor info
         JSR C30EFD      ; Queue text upload
         LDX #$2D66      ; Next: Fade-in
         STX $26         ; Queue: Choice 1
         JMP C33541      ; BRT: 1 + NMI

; Initialize variables for Lineup menu
C370FC:  JSR C3352Y      ; Reset/Stop stuff
;        LDA #$03        ; 64x64 at $0000
;        STA $2107       ; Set BG1 map loc
         LDA #$06        ; Status icons: Temp
         TSB $47         ; Set menu flag
C36904:  LDY $00         ; Clear Y
         STY $41         ; List V-Spd: 0
         STY $37         ; BG1 Y-Loc: 0
         STY $3B         ; BG2 Y-Loc: 0
         STY $3F         ; BG3 Y-Loc: 0
C3690X:  STY $35         ; BG1 X-Loc: 0
C3690Y:  STY $39         ; BG2 X-Loc: 0
         STY $3D         ; BG3 X-Loc: 0
C36914:  RTS             ; Keep Y unchanged

; 44: Return to Lineup menu from Status menu
C37131:  JSR C370FC      ; Init variables
         JSR C37677      ; Create/lock chars
         BRA C37114      ; Draw menu, etc.

; 2D: Handle Lineup menu choice 1
C37168:  JSR C371B9      ; Cover D-Pad/stats
         BCC C36914      ; If crossed center
         LDA $08         ; Pushing A?
         BPL C371A9      ; Branch if not
         JSR lnp_cur     ; Cursor slot
         STX $28         ; Set as pick 1
         LDA $7EAC8D,X   ; Locked slot?
         BMI C371A2      ; Fail if so
C3716_:  INC $26         ; Menu mode +1
         JSR C30EB2      ; Sound: Click
         JMP C32F21      ; Create blinker

; Fork: Forced member
C371A2:  JMP C3B66F      ; Buzzer + Mosaic

; Fork: Handle attempt to exit menu
C371A9:  LDA $09         ; No-autofire keys
         BIT #$90        ; Using Start or B?
         BEQ C36914      ; Exit if not
C37296:  TDC             ; Clear A
         TAX             ; Slot index: 0
         JSR ptycnt      ; Group count
C372A3:  PHA             ; Save counter
         CLC             ; Nobody: Y
         LDY #$0004      ; Slots left: 4
C372A8:  LDA $7E9D99,X   ; Empty slot?
         BMI C372B0      ; Branch if so
         SEC             ; Nobody: N
C372B0:  INX             ; Slot index +1
         DEY             ; One less slot
         BNE C372A8      ; Loop till 4th
         PLA             ; Parties left
         BCC C372CB      ; Fail if nobody
         DEC A           ; One less left
         BPL C372A3      ; Loop till last
;        STZ $0205       ; Never read...
         LDY #$FF67      ; Next: Fade-out
         JMP C3254_      ; Sound + Exit

; Fork: Draw "You need <#> group(s)."
C372CB:  JSR C30EC0      ; Play buzzer
         JSR ptycnt      ; One group?
         BEQ C372E9      ; Branch if so
         LDY #C37AB7     ; Text pointer
         JSR draw20      ; Draw message
         LDX #$392F      ; Tilemap pointer
         JSR C375F7      ; Draw group count
         BRA C372EF      ; Skip 2 lines

; Fork: Draw "No one there!"
C372E9:  LDY #C37ACE     ; Text pointer
         JSR draw20      ; Draw message
C372EF:  LDA #$2C        ; C3/7A87
         JMP C3B87F      ; Next: Wait

; Handle D-Pad and update actor info in Lineup menu
C371B9:  JSR C375B8      ; Update char info
         LDA $4D         ; Cursor column
         STA $5E         ; Set as former
         LDA $4A         ; In party area?
         BNE C371DE      ; Branch if so
         LDA $0B         ; Semi-auto keys
         BIT #$04        ; Pushing down?
         BEQ C37206      ; Branch if not
         LDA $4E         ; On row 2?
         BEQ C37206      ; Branch if not

; Fork: Enter party area
C371CC:  LDA #$10        ; Cursor slot: +16
         STA $4A         ; Set area mod
         JSR C373DB      ; Relocate cursor
         BRA C37201      ; Sound: Cursor

; Fork: Handle up
C371DE:  JSR key_up      ; Pushing up?
         BEQ C37206      ; Branch if not
         LDA $4E         ; On row 3?
         BNE C37206      ; Branch if not

; Fork: Exit party area
C371EC:  JSR C37482      ; Load navig data
         INC $4E         ; Cursor row: 2nd
         TDC             ; Clear A
         LDA $5E         ; Former column
         TAX             ; Index it
         LDA.L C37476,X  ; Intended column
         STA $4D         ; Set new column
         JSR C3748B      ; Relocate cursor
C37201:  CLC             ; Mark area exit
         JMP C30EA3      ; Sound: Cursor

; Fork: Staying within same area
C37206:  JSR C37415      ; Handle D-Pad
         SEC             ; Unmark area exit
C3720B:  RTS

; New cursor column upon leaving 3-party area of Lineup menu
C37476:  db $00          ; 1 -> 1
         db $01          ; 2 -> 2
         db $03          ; 3 -> 4
         db $04          ; 4 -> 5
         db $06          ; 5 -> 7
         db $07          ; 6 -> 8

; 2E: Handle Lineup menu choice 2
C3720C:  JSR C371B9      ; Cover D-Pad/stats
         BCC C3720B      ; If crossed center
         LDA $08         ; Pushing A?
         BPL C3727D      ; Branch if not
         JSR C30EB2      ; Sound: Click
         JSR lnp_cur     ; X = Cursor slot
         LDA $7EAC8D,X   ; Locked slot?
         BMI C3728F      ; Fail if so
         TXA             ; Cursor slot B
         CMP $28         ; Same as slot A?
         BNE C37265      ; Swap chars if not

; Fork: Double-clicked a slot
C37234:  JSR sk_slt5     ; Empty slot?
         BMI C3728F      ; Fail if so
         LDY #$4267      ; Next: Fade-out
         STY $26         ; Queue Status menu
C37264:  RTS

; Fork: Swap slot contents
C37265:  LDA #$07        ; Actors: Delete
         TRB $47         ; Set menu flag
         JSR C372F8      ; Swap positions
         JSR C311B0      ; Delete actors
         JSR C37683      ; Create actors
         BRA C37286      ; Next: Choice 1

; Fork: Handle B
C3727D:  LDA $09         ; Pushing B?
         BPL C37264      ; Exit if not
         JSR C30EA9      ; Sound: Cursor
C37286:  DEC $26         ; Next: Choice 1
         JMP C343C_      ; Blinker: Off

; Fork: Invalid selection
C3728F:  JMP C3B66F      ; Buzzer + Mosaic

; Swap actor positions in Lineup menu
C372F8:  LDA $28         ; Chosen slot A
         JSR sk_slt5     ; Actor in slot A
         STA $E3         ; Memorize it
         PHX             ; Memorize slot
         LDA.L C373AF,X  ; Party position
         STA $E0         ; Memorize it

; Fork: Get actor data for slot B
C37326:  JSR lnp_cur     ; Actor in slot B
         STA $E2         ; Memorize it
         PHX             ; Memorize slot
         LDA.L C373AF,X  ; Party position
         STA $E1         ; Memorize it

; Fork: Switch data for both actors
C3737A:  LDY #$0001      ; 1st: Actor B
.loop    PLX             ; Chosen slot X
         LDA $00E2,Y     ; Actor in slot Y
         JSR st_9D89     ; Put in slot X
         BMI .next       ; Skip if nobody
         TAX             ; Index it
         LDA $1850,X     ; Actor info
         AND #$20        ; Isolate row
         ORA $00E0,Y     ; Add new position
         STA $1850,X     ; Save changes
.next    DEY             ; Actor index -1
         BPL .loop       ; Loop till last
         RTS

; Party and roster position for Lineup menu slots
C373AF:  db $40          ; Non-party slot 1
         db $40          ; Non-party slot 2
         db $40          ; Non-party slot 3
         db $40          ; Non-party slot 4
         db $40          ; Non-party slot 5
         db $40          ; Non-party slot 6
         db $40          ; Non-party slot 7
         db $40          ; Non-party slot 8
         db $40          ; Non-party slot 9
         db $40          ; Non-party slot 10
         db $40          ; Non-party slot 11
         db $40          ; Non-party slot 12
         db $40          ; Non-party slot 13
         db $40          ; Non-party slot 14
         db $40          ; Non-party slot 15
         db $40          ; Non-party slot 16
         db $41          ; Party 1, NW
         db $49          ; Party 1, SW
         db $51          ; Party 1, NE
         db $59          ; Party 1, SE
         db $42          ; Party 2, NW
         db $4A          ; Party 2, SW
         db $52          ; Party 2, NE
         db $5A          ; Party 2, SE
         db $43          ; Party 3, NW
         db $4B          ; Party 3, SW
         db $53          ; Party 3, NE
         db $5B          ; Party 3, SE

; Load nav data for party area of Lineup menu, relocate cursor
C373DB:  JSR ptycnt      ; One group?
         BEQ C373F5      ; Branch if so
         DEC A           ; Two groups?
         BEQ C37405      ; Branch if so

; Fork: Three groups
C373EC:  LDY #C37505     ; C3/7505
         JSR C305FE      ; Load navig data
         TDC             ; Clear A
         LDA $5E         ; Former column
         TAX             ; Index it
         LDA.L C37466,X  ; Intended column
         STA $4D         ; Set new column
         BRA C374B8      ; Relocate cursor

; Fork: One group
C373F5:  LDY #C374E3     ; C3/74E3
         JSR C305FE      ; Load navig data
         LDA $5E         ; Old cursor column
         BEQ C37400      ; Retain if first
         LDA #$01        ; New column: 2nd
C37400:  BRA C37410      ; Relocate cursor

; Fork: Two groups
C37405:  LDY #C374F0     ; C3/74F0
         JSR C305FE      ; Load navig data
         LDA $5E         ; Old cursor column
         CMP #$03        ; Within first 3?
         BCC C37410      ; Retain it if so
         LDA #$03        ; Column: 4th
C37410:  STA $4D         ; Set new column
         BRA C374A9      ; Relocate cursor

; New cursor column upon entering 3-party area of Lineup menu
C37466:  db $00          ; 1 -> 1
         db $01          ; 2 -> 2
         db $01          ; 3 -> 2
         db $02          ; 4 -> 3
         db $03          ; 5 -> 4
         db $04          ; 6 -> 5
         db $04          ; 7 -> 5
         db $05          ; 8 -> 6

; Handle D-Pad for Lineup menu
C37415:  LDA $4A         ; In party area?
         BEQ C3742F      ; Branch if not
         JSR ptycnt      ; One group?
         BEQ C3742C      ; Branch if so
         DEC A           ; Two groups?
         BEQ C37429      ; Branch if so

; Fork: Area with three groups
C374B5:  JSR C3072D      ; Handle D-Pad
C374B8:  LDY #C3750A     ; C3/750A
         JMP C30637      ; Relocate cursor

; Fork: Area with two groups
C37429:  JSR C3072D      ; Handle D-Pad
C374A9:  LDY #C374F5     ; C3/74F5
         JMP C30637      ; Relocate cursor

; Fork: Area with one group
C3742C:  JSR C3072D      ; Handle D-Pad
C3749A:  LDY #C374E8     ; C3/74E8
         JMP C30637      ; Relocate cursor

; Fork: Non-party area
C3742F:  JSR C3072D      ; Handle D-Pad
C3748B:  LDY #C374C3     ; C3/74C3
         JMP C30640      ; Relocate cursor

; Draw "Form <#> group(s)."
C37562:  LDY #C37A95     ; Text pointer
         JSR draw20      ; Draw "Form..."
         LDX #$3927      ; Tilemap pointer
C375F7:  JSR ptycnt      ; Group count
         JMP C3410F      ; Draw value

; Create actors in Lineup menu
C37683:  PHB             ; Save DB
         LDX #$9D99      ; Src: 7E/9D99
         STX $2181       ; Set WRAM LBs
         STZ $E4         ; Base X: 0
         JSR ptycnt      ; Group count

; Fork: Handle current party
C3787F:  PHA             ; Save counter
         LDX $00         ; Member slot: 1
C37881:  LDA $002180     ; Actor in slot
         BMI C378C0      ; Skip if nobody
         PHA             ; Memorize it
         PHX             ; Save slot
         LDY #C37B42     ; C3/7B42
         JSR C378EB      ; Queue OAM fn
         PLX             ; Member slot
         JSR D8_35CA     ; Ptr HB: D8
         LDA.L C378E7,X  ; Y position
         STA $344A,Y     ; Set ally's
         XBA             ; Save for arrow
         LDA.L C378E3,X  ; Relative X
         CLC             ; Prepare ADC
         ADC $E4         ; Add base X
         STA $33CA,Y     ; Set ally's X
         TAY             ; Coordinates
         TDC             ; Clear A
         PLA             ; Actor in slot
         PHX             ; Save member slot
         JSR C37BAE      ; Add Lineup arrow
         PLX             ; Member slot
C378C0:  INX             ; Member slot +1
         CPX #$0004      ; Done all 4?
         BNE C37881      ; Loop if not

; Fork: Handle next party
C378C6:  LDA $E4         ; Base X pos
         ADC #$4F        ; Go 80 px right
         STA $E4         ; Save changes
         PLA             ; Groups left
         DEC A           ; One less left
         BPL C3787F      ; Loop till last

; Fork: Create candidates
         LDX #$000F      ; Menu slot: Last
C37685:  TDC             ; Clear A
         JSR sk_slt6     ; Actor in slot
         BMI C376BF      ; Skip if none
         PHX             ; Save slot
         LDY #C37B42     ; C3/7B42
         JSR C378EB      ; Queue OAM fn
         PLX             ; Menu slot
         TXA             ; Put in A
         AND #$07        ; Get column
         XBA             ; Put in B
         LDA #$1C        ; Spacing H-wise
         JSR mul8        ; Column x Spacing
         ADC #$18        ; Add base X
         STA $33CA,Y     ; Set X position
         LDA #$5C        ; Y: 92
         CPX #$0008      ; In bottom row?
         BCC .set_y      ; Branch if not
         LDA #$78        ; Y: 120
.set_y   STA $344A,Y     ; Set Y position
         JSR D8_35CA     ; Ptr HB: D8
C376BF:  DEX             ; Menu slot -1
         BPL C37685      ; Loop till < 0
         PLB             ; Restore DB
         RTS

; List and create actors in Lineup menu, define locked slots
C3762A:  LDX #$009F      ; Menu slot
C3762E:  JSR st_9D8_     ; Clear slot
         DEX             ; Menu slot -1
         BPL C3762E      ; Loop till < 0
         LDA $0201       ; Forced members?
         BPL C37643      ; Branch if so

; Fork: Clear assigned party for all actors
C37665:  LDX #$000F      ; 1st: Guest 2
C37667:  LDA $1850,X     ; Party info
         AND #$F8        ; Clear party
         STA $1850,X     ; Save changes
         DEX             ; Actor slot -1
         BPL C37667      ; Loop till < 0

; Fork: Compile candidates
C37643:  JSR wm9D89      ; WRAM: 7E/9D89
         LDX $00         ; 1st: Terra
C37645:  LDA $1850,X     ; Party info
         ASL A           ; Object's off?
         BPL C37657      ; Skip if so
         AND #$0E        ; In a party?
         BNE C37657      ; Skip if so
         TXA             ; Actor's slot
         STA $2180       ; Add to list
C37657:  INX             ; Actor slot +1
         CPX #$0010      ; Done all 16?
         BNE C37645      ; Loop if not
         JSR C3C13B      ; End list

; Fork: List members, create actors, define locked slots
C37677:  LDY #$000F      ; 1st: Guest 2
C37701:  TDC             ; Clear A
         LDA $1850,Y     ; Party info
         PHA             ; Memorize it
         AND #$18        ; Roster slot
         LSR A           ; Put in b2-b3
         LSR A           ; Put in b1-b2
         LSR A           ; Now 0-3
         STA $E0         ; Memorize it
         PLA             ; Party info
         ASL A           ; Object's off?
         BPL C37715      ; Skip if so
         AND #$06        ; Isolate party
         BEQ C37715      ; Skip if none
         ASL A           ; Party x4
         ORA $E0         ; Add roster slot
         TAX             ; Index sum
         TYA             ; Actor slot
         STA $7E9D99-4,X ; Add to list
C37715:  DEY             ; Actor slot -1
         BPL C37701      ; Loop till < 0
         JSR C37683      ; Create actors

; Fork: Establish locked actor slots
C3790C:  LDX #$002B      ; Menu slot: Last
C37914:  PHX             ; Save menu slot
         TDC             ; Clear A
         JSR sk_slt6     ; Actor in slot
         INC A           ; None?
         BEQ C37933      ; Unlock if so
         DEC A           ; Restore value
         JSR forced      ; Forced member?
         BEQ C37933      ; Unlock if not
         LDA #$FF        ; Slot: Locked
C37933:  PLX             ; Current slot
         STA $7EAC8D,X   ; Set slot status
         DEX             ; Menu slot -1
         BPL C37914      ; Loop till < 0
         RTS

; Create actor sprite for Lineup menu
C378EB:  PHA             ; Save actor
         JSR qanim2      ; Queue OAM fn
;        TDC             ; Clear A
         PLA             ; Actor
         STA $7E35C9,X   ; Put in queue
         TXY             ; Queue index
         JSR charadr     ; Actor's address
         TDC             ; Clear A
         LDA $01,X       ; Sprite set
C378FA:  ASL A           ; Double it
         TAX             ; Index it
         JSR db7e        ; Set DB to 7E
         REP #$20        ; 16-bit A
         LDA $D8E917,X   ; Anim table ptr
C37906:  STA $32C9,Y     ; Set sprite's
         SEP #$20        ; 8-bit A
         RTS

; Initialize and load portrait for Lineup menu
C3793F:  JSR C361DA      ; Queue OAM fn
         JSR db7e_       ; Set DB to 7E
         TAY             ; Face slot: 1
         JSR C30AFF      ; X: 26
         JSR C30ACC      ; Load anim ptr
         JSR C31206      ; Set pose timer
         PLB             ; Restore DB
         LDA #$2F        ; Y: 47
         JSR C3EA0Z      ; Set object's
         JSR C314AC      ; Upload graphics

; Fork: Set to upload portrait
C3794D:  JSR lnp_cur     ; Actor to show
         BMI C37CA4      ; Blank if none
         JSR charadr     ; Actor's address
         TXY             ; Copy it to Y
         REP #$20        ; 16-bit A
         LDX $00         ; VRAM ptr index
         JSR C36EBX      ; Set src, dst

; Fork: Load portrait palette
C37CAE:  JSR lnp_cur     ; Actor to show
         JSR gfxset1     ; Graphics set
         JMP C3678X      ; Load colors

; Fork: Set to upload blank portrait
C37CA4:  LDY #$9F51      ; 7E/9F51
         STY $1D         ; Set src LBs
         JMP C3A60C      ; Src HB: 7E

; 67: Queue fade-out for Lineup menu
C379F0:  LDA #$68        ; Next: Wait
         JSR C31D7_      ; Queue fade-out
         BRA C375B8      ; Update char info

; 66: Queue fade-in for Lineup menu
C379FF:  LDA #$68        ; Next: Wait
         JSR C31D80      ; Queue fade-in
         BRA C375B8      ; Update char info

; 68: Wait while Lineup menu fades in or out
C37A0E:  JSR C31D8B      ; Handle timer
C375B8:  LDA #$06        ; Status icons: Temp
         TSB $47         ; Set menu flag
         JSR C3794D      ; Handle portrait
         JSR C37CAE      ; Load face colors

; Fork: Draw actor info
C37953:  LDX #$3A09      ; Tilemap ptr
         LDY #$0180      ; Bytes: 384
         JSR C3A79_      ; Erase BG1 text
         JSR lnp_cur     ; Actor to show
         BMI C37906      ; Exit if nobody
         STA $C9         ; Memorize it
         JSR charadr     ; Actor's address
         STX $67         ; Memorize it
         JSR txt24       ; Color: Blue
         LDX #C379C9     ; Text ptrs loc
         JSR drawx3      ; Draw "LV/HP/MP"
         LDY #$3A5B      ; Class/KO pos
         LDX #$3048      ; Icon position
         LDA #$02        ; Icon anim mode
         JSR C3625B      ; Display status
         LDY #$3ADB      ; Text position
         JSR C334CF      ; Draw actor name
         LDY #$3B5B      ; Text position
         JSR C334E6      ; Draw held esper
         LDX #C379D_     ; Text ptrs loc
         JSR C39063      ; Draw both "/"
         LDX #C379E6     ; Coords tbl ptr
         JMP C30C6C      ; Draw LV, HP, MP

; 2C: Erase warning in Lineup menu after delay
C37A87:  LDA $20         ; Delay expired?
         BNE C37A92      ; Branch if not
         INC $26         ; Next: Choice 1
         JSR C37562      ; Draw "Form..."
C37A92:  BRA C375B8      ; Update char info

; Update arrow for gear data menu
C37A19:  BNE C37A3E      ; If mode 1

; Mode 0: Initialize arrow
C37A21:  JSR C3120_      ; Set pose timer

; Mode 1: Sustain arrow
C37A3E:  CLC             ; Preset to dequeue
         LDA $99         ; Arrow's status
         BMI C37B9B      ; Delete if off
         REP #$20        ; 16-bit A
         LDA #$EC6F      ; Anim table ptr
         JMP C339BD      ; Set OAM; requeue

; Update conditional actor-related sprite
C37B42:  CMP #$04        ; Mode 2?
         BEQ C37B66      ; Branch if so
         BCS C37B8D      ; If mode 3
         LSR A           ; Mode 1?
         BNE C37B7E      ; Branch if so

; Mode 0: Init Lineup actor or arrow, non-Lineup status icon, Colosseum candidate, or enemy Shadow
C37B52:  LDA #$01        ; Ax like OBJs: N
         TSB $47         ; Set menu flag
         JSR C3120_      ; Set pose timer

; Mode 1: Sustain Lineup actor or arrow, non-Lineup status icon, Colosseum candidate, or enemy Shadow
C37B7E:  LDA $47         ; Menu flags
         LSR A           ; Ax like objects?
         BCC C37B9B      ; Delete if so
         JMP C30860      ; Set OAM; requeue

; Mode 2: Init Colosseum challenger or Lineup status icon
C37B66:  JSR C3120_      ; Set pose timer

; Mode 3: Sustain Colosseum challenger or Lineup status icon
C37B8D:  JSR C319CC      ; Define OAM
;        CLC             ; Preset to dequeue
         LDA $47         ; Menu flags
         AND #$02        ; Temp sprite?
         BNE C37B9B      ; Exit if so
         SEC             ; Set to requeue
C37B9B:  RTS

; Create large arrow under actor if forced into party
C37BAE:  JSR forced      ; Forced member?
         BEQ C37B9B      ; Exit if not
         PHY             ; Push position
         LDY #C37B42     ; C3/7B42
         JSR qanim3      ; Queue OAM fn
         PEA $EBA9       ; Anim table ptr
         JMP C3859Y      ; Set arrow data

; Set to condense text in Item, Colosseum, or Sell menu
C37E2B:  JSR C37E95      ; Load BG1 V-Data
         LDX #$000C      ; H-Pan data index
C37E4E:  LDA.L C37EDA,X  ; BG1 H-Pan data
         STA $7E9A09,X   ; Save in RAM
         DEX             ; Index -1
         BPL C37E4E      ; Loop till done
         LDY #C37F57     ; Src: C3/7F57
dma7a:   PEA $9A09       ; BG1h src: 7E/9A09
         JMP dma7b       ; Queue BG1 H-Pan

; Load BG1 vertical shift table for Item, Colosseum, or Sell menu
C37E95:  LDX #$006F      ; Data index
C37E97:  LDA.L C37EE7,X  ; V-Shift data
         STA $7E9849,X   ; Save in RAM
         DEX             ; Index -1
         BPL C37E97      ; Loop till < 0
         LDX #$0057      ; Data index
C37EA5:  JSR topbg1      ; BG1 top row
         ADC.L C37EF9+1,X; Add V-Pan data
         STA $7E985C,X   ; Save in RAM
         SEP #$20        ; 8-bit A
         DEX             ; Index -1
         DEX             ; Index -1
         DEX             ; Index -1
         BPL C37EA5      ; Loop till < 0
         RTS

; Draw item's name, quantity owned, and class
C37FA1:  TDC             ; Clear A
         LDA $E5         ; Item slot
         TAY             ; Index it
         JSR C38045      ; Choose palette
         LDA $E6         ; BG1 write row
         INC A           ; Go 1 row down
         LDX #$0011      ; X: 17
         JSR C3809F      ; Compute map ptr
         SEP #$20        ; 8-bit A
         LDA $1969,Y     ; Item quantity
         JSR C304B_      ; Draw 2 digits
         LDX #$0003      ; X: 3
         JSR sk_slt1     ; Compute map ptr
         TXY             ; Item slot
         JSR C380B9      ; Load "<item>:"
         JSR C38847      ; Draw string

; Fork: Draw item class
C37FE6:  REP #$21        ; 16-bit A; C-
         LDA [$E7]       ; Text position
         ADC #$0024      ; Skip item name
         JSR C3BAB3      ; Save changes
;        TDC             ; Clear A
         LDX.W #!itm_cs  ; Letters: 7
         LDA $7E9E8B     ; Item icon
         INC A           ; None?
         BEQ C3802C      ; Blank if so
         SEC             ; Prepare SBC
         SBC #!icon+1    ; Now 0-15
         PHA             ; Save class
         LDA.B #!itm_cp!h; Text ptr HB
         LDY.W #!itm_cp  ; Text ptr LBs
         JMP C35EE_      ; Draw class

; Fork: Blank item class
C3802C:  JMP C357F3      ; Draw 7 spaces

; Choose item's color for Item, Colosseum, or Sell menu
C38045:  LDA $0200       ; In main menu?
         BNE C38085      ; Enable if not
         LDA $1869,Y     ; Item in slot
         CMP #$F7        ; Tent?
         BEQ C3808F      ; Branch if so
         CMP #$F6        ; Sleeping Bag?
         BEQ C3808F      ; Branch if so
         CMP #$FD        ; Warp Stone?
         BEQ C38096      ; Branch if so
         JSR C38321      ; Compute index
         LDA $D85000,X   ; Properties
         ASL A           ; Usable in field?
C38083:  BPL C3808A      ; Disable if not
C38085:  LDA #$20        ; Palette 0
         BRA C3808C      ; Color: User's

; Fork: Tent or Sleeping Bag
C3808F:  LDA $0201       ; Can save here?
         BRA C38083      ; Enable if so

; Fork: Warp Stone
C38096:  LDA $0201       ; Area permissions
         BIT #$02        ; Can warp here?
         BNE C38085      ; Enable if so
C3808A:  LDA #$28        ; Palette 2
C3808C:  STA $29         ; Color: Gray
         RTS

; Set text string to use palette 1
txt24:   LDA #$24        ; Palette 1
         BRA C3808C      ; Set color

; Set text string to use palette 4
txt30:   LDA #$30        ; Palette 4
         BRA C3808C      ; Set color

; Compute position into BG1 tilemap A
C3809F:  PHA             ; Push Y pos
         TDC             ; Clear A
         PLA             ; Pull Y pos
         JSR x64         ; x64
         STA $E7         ; Memorize it
         TXA             ; X position
         ASL A           ; Double it
         ADC $E7         ; Add Y x 64
         ADC #$3849      ; Add base adr
         TAX             ; Put sum in X
         RTS

; Update scroll parameters for list
C38183:  LSR A           ; Mode 0?
         BEQ C3818F      ; Branch if so
         DEC A           ; Mode 2?
         BNE C381B0      ; Branch if so

; Mode 1: Scroll downward
C3819C:  LDY #$0004      ; 4 px down
         BRA C381A3      ; Set V-Speed

; Mode 0: Scroll upward
C3818F:  LDY #$FFFC      ; 4 px up
C381A3:  STY $41         ; Set V-Speed
         LDA #$02        ; Mode: 2
         STA $3649,X     ; Set next mode
         INC A           ; Frames: 3
         STA $20         ; Set scroll timer

; Mode 2: Check timer
C381B0:  LDA #$20        ; BG1 scroll flag
         TSB $46         ; Scroll BG1: Y
         SEC             ; Preset to requeue
         LDY $20         ; Timer expired?
         BNE C381C6      ; Exit if not

; Fork: Stop scrolling
C381BC:  STY $41         ; BG1 V-Speed: 0
         TRB $46         ; Scroll BG1: N
         CLC             ; Set to dequeue
C381C6:  RTS

; Handle D-Pad for scrollable list, change row when wrapping
C381C7:  LDA $20         ; Scrolling now?
         BNE C381C6      ; Exit if so

; Fork: Handle up
C381CB:  JSR key_up      ; Pushing up?
         BEQ C381EA      ; Branch if not
         LDA $4E         ; On first row?
         BNE C3821X      ; Go up if not
         LDA $4A         ; At top of list?
         BNE C3822X      ; Scroll if not
         RTS

; Fork: Handle down
C381EA:  LDA $0B         ; Semi-auto keys
         BIT #$04        ; Pushing down?
         BEQ C38210      ; Branch if not
         LDA $54         ; Bottom menu row
         DEC A           ; Adjust value
         CMP $4E         ; Cursor's there?
         BNE C3824X      ; Go down if not
         LDA $4A         ; Scroll position
         CMP $5C         ; At very bottom?
         BNE C3826X      ; Scroll if not
C38285:  RTS

; Fork: Handle left
C38210:  LDA $0B         ; Semi-auto keys
         BIT #$02        ; Pushing left?
         BEQ C38249      ; Branch if not
         LDA $4D         ; On first column?
         BNE C38241      ; Branch if not
         LDA $4E         ; On top row?
         BEQ C3822D      ; Branch if so
         LDA $53         ; Furthest column
         DEC A           ; Adjust value
         STA $4D         ; Set as cursor's
         STA $4F         ; Set as list's
C3821X:  DEC $4E         ; Cursor row -1
         BRA C3822Y      ; Branch below

; Fork: Wrap to upper, offscreen row
C3822D:  LDA $4A         ; At top of list?
         BEQ C381C6      ; Exit if so
         LDA $53         ; Furthest column
         DEC A           ; Adjust value
         STA $4D         ; Set as cursor's
         STA $4F         ; Set as list's

; Fork: Set to scroll upward, draw row to scroll into view
C3822X:  DEC $4A         ; Scroll pos -1
         DEC $49         ; Top BG1 row -1
         JSR C383F7      ; Define $E5, $E6
         JSR C382DD      ; Draw new row
         LDY #C38183     ; C3/8183
         JSR qanim0      ; Queue scroll fn
;        TDC             ; Anim mode: Up
         JSR C382D8      ; Set scroll mode
C3822Y:  DEC $50         ; List row -1
         BRA C38282      ; Sound: Cursor

; Fork: Move cursor left
C38241:  DEC $4D         ; Cursor column -1
         DEC $4F         ; List column -1
         BRA C38282      ; Sound: Cursor

; Fork: Handle right
C38249:  LDA $0B         ; Semi-auto keys
         LSR A           ; Pushing right?
         BCC C38285      ; Exit if not
         LDA $53         ; Furthest column
         DEC A           ; Adjust value
         CMP $4D         ; Cursor's there?
         BNE C3827E      ; Go right if not
         LDA $54         ; Bottom menu row
         DEC A           ; Adjust value
         CMP $4E         ; Cursor's there?
         BEQ C3826A      ; Branch if so
         STZ $4D         ; Cursor column: 1
         STZ $4F         ; List column: 1
C3824X:  INC $4E         ; Cursor row +1
         BRA C3826Y      ; Branch below

; Fork: Wrap to lower, offscreen row
C3826A:  LDA $4A         ; Scroll position
         CMP $5C         ; At very bottom?
         BEQ C38285      ; Exit if so
         STZ $4D         ; Cursor column: 1
         STZ $4F         ; List column: 1
C3826X:  JSR C3829E      ; Queue scrolling
C3826Y:  INC $50         ; List row +1
         BRA C38282      ; Sound: Cursor

; Fork: Move cursor right
C3827E:  INC $4D         ; Cursor column +1
         INC $4F         ; List column +1
C38282:  JMP C30EA3      ; Sound: Cursor

; Set to scroll list downward, draw row to scroll into view
C3829E:  LDA $5A         ; Rows per page
         CLC             ; Prepare ADC
         ADC $4A         ; Add scroll pos
         JSR C383F_      ; Set SW list slot
         LDA $49         ; Top BG1 write row
         ADC $5A         ; Get bottom row +1
         ASL A           ; x2 for blank rows
         AND #$1F        ; Stay in limits
         STA $E6         ; Set BG1 write row
         INC $4A         ; Scroll pos +1
         INC $49         ; Top BG1 row +1
         JSR C382DD      ; Draw new row
         LDY #C38183     ; C3/8183
         JSR qanim0      ; Queue scroll fn
C382D6:  LDA #$01        ; Anim mode: Down
C382D8:  STA $7E3649,X   ; Set scroll mode
         RTS

; Draw list elements to scroll into view
C382DD:  TDC             ; Clear A
         LDA $2A         ; List type
         ASL A           ; Double it
         TAX             ; Index it
         JMP (C382E5,X)  ; Draw row

; Jump table for the above
C382E5:  dw C37FA1       ; Inventory
         dw C34F9E       ; Magic
         dw C35256       ; Lore
         dw C353EE       ; Rage
         dw C354E3       ; Esper
         dw C39CE2       ; Gear

; Prepare item description loading
C38308:  LDY.W #!itm_d_p ; Ptr loc: ED/7AA0
         LDX.W #!itm_d_l ; Txt loc: ED/6400
         LDA.B #!itm_d_h ; Bank: ED
         JSR C35C0_      ; Set pointers
C3831A:  LDX #$9EC9      ; 7E/9EC9
         JMP wm_set      ; Set WRAM LBs

; Compute item data index
C38321:  XBA             ; Put item in B
         LDA #$1E        ; Item data size
         JMP C350F_      ; Item x size

; Load common item's description and draw item count in Item menu
C382F1:  JSR C3B4F8      ; Handle desc
         LDX #$00FF      ; Item slot: 256
         LDY $00         ; Item count: 0
C38359:  LDA $1869,X     ; Item in slot
         INC A           ; None?
         BEQ C38361      ; Exclude if so
         INY             ; Item count +1
C38361:  DEX             ; Item slot -1
         BPL C38359      ; Loop till < 0

; Draw number of common or rare items
C3837A:  JSR C38085      ; Color: User's
         TYA             ; Item count
         LDX #$7ABF      ; Text position
         JMP C304CX      ; Draw 3 digits

; Load rare item's description
C38339:  LDY.W #!rar_d_p ; Ptr loc: CE/FB60
         LDX.W #!rar_d_l ; Txt loc: CE/FCB0
         LDA.B #!rar_d_h ; Bank: CE
         JMP C3571Y      ; Load description

; Draw rare item list and item count
C3838B:  JSR C36A15      ; Clear BG1 map A

; Fork: Build rare item list, draw item count
C38394:  LDY #$1EBA      ; Item packs adr
         LDA #$02        ; Item packs: 3
         JSR C353C1      ; Build list
         JSR wm9D89      ; WRAM: 7E/9D89
         LDY $00         ; Item count: 0
         TYX             ; Item slot: 1
.loop    JSR sk_slt6     ; Item in slot
         BMI .next       ; Skip if none
         PHA             ; Save item
         JSR st_9D8_     ; Clear slot
         PLA             ; Item number
         STA $2180       ; Add to list
         INY             ; Item count +1
.next    INX             ; Item slot +1
         CPX #$0014      ; Done 20 slots?
         BNE .loop       ; Loop if not
         JSR C3837A      ; Draw item count

; Fork: Draw rare item list
C383DE:  JSR C383F7      ; Set BG1 WR row
         STZ $E5         ; Item slot: 1
         LDA #$0A        ; Rows left: 10
C383E6:  PHA             ; Save counter
         JSR C3841B      ; Draw 2 items
         JSR e6add2      ; BG1 WR row +2
         PLA             ; Rows left
         DEC A           ; One less left
         BNE C383E6      ; Loop till last
         RTS

; Define BG1 row for drawing list, plus first onscreen list slot
C383F7:  LDA $49         ; Top BG1 write row
         ASL A           ; x2 for blank rows
         AND #$1F        ; Stay in limits
         STA $E6         ; Set BG1 write row
         LDA $4A         ; Scroll position
C383F_:  XBA             ; Put in B
         LDA $5B         ; Number of columns
         JSR mul8        ; B * A
         STA $E5         ; Set list slot
         RTS

; Draw a row of two SwdTechs
C35311:  LDX #$0003      ; X: 3
         JSR C3531C      ; Draw tech A
         LDX #$0011      ; X: 17
C3531C:  JSR C35328      ; Define source
         BRA C357BB      ; Draw Tech name

; Draw a row of two Dance names
C357AA:  LDX #$0003      ; X: 3
         JSR C357B5      ; Draw Dance A
         LDX #$0011      ; X: 17
C357B5:  JSR C357C1      ; Define source
C357BB:  JSR sk_slt2     ; Dance in slot
         BRA C3842_      ; Draw name

; Draw a row of two rare items
C3841B:  LDX #$0003      ; X: 3
         JSR C3842A      ; Draw item A
         LDX #$0011      ; X: 17
C3842A:  JSR C38436      ; Define source
         JSR sk_slt1     ; Item in slot
C3842_:  BMI C38433      ; Skip if none
         JSR C334FY      ; Draw name
C38433:  INC $E5         ; Item slot +1
         RTS

; Load fixed-size string from table
C3846_:  JSR wm9E8B      ; WRAM: 7E/9E8B
         XBA             ; String number
         LDX $EB         ; String size
         TXA             ; Set multiplier
         JSR mul8        ; Number x size
         TAY             ; Index product
C3848A:  LDA [$EF],Y     ; Letter
         STA $2180       ; Add to string
         INY             ; Point to next
         DEX             ; One less left
         BNE C3848A      ; Loop till last
         RTS

; Disable scrollbar and freeze description
C3858C:  LDA #$C0        ; Scrollbar: Off
C3858E:  TRB $46         ; Set anim index
C38590:  LDA #$10        ; Reset/Stop desc
         TSB $45         ; Set menu flag
         RTS

; Add " can be used by:" after item name
C385AD:  LDX.W #!itm_ns-1; Letter index
C385B0:  DEX             ; Letter index -1
         LDA $7E9E8C,X   ; Item letter
         INC A           ; Terminator?
         BEQ C385B0      ; Loop if so
         LDY $00         ; ROM index: 0
C385BA:  INX             ; RAM index +1
         PHX             ; Save RAM index
         TYX             ; ROM index
         LDA.L C38D34,X  ; Message letter
         PLX             ; RAM index
         STA $7E9E8C,X   ; Add to string
         INY             ; ROM index +1
         LSR A           ; At terminator?
         BNE C385BA      ; Loop if not
         RTS

; Filter and draw gear's list of compatible actors
C385D5:  LDX $00         ; List slot: 1
         STX $E5         ; Name slot: 1
C385DD:  JSR sk_slt6     ; Eligible actor
         BMI C38652      ; Exit if terminator
         PHX             ; Save list slot
         LDX #$0020      ; 1st: Guest 2 (+1)
C3860A:  DEX             ; Actor index -1
         DEX             ; Actor index -1
         BMI C3861B      ; Branch if < 0
         PHA             ; Memorize actor
         PHX             ; Save index
         JSR charadY     ; Actor's address
         TXY             ; Copy to Y
         STY $67         ; Memorize it
         PLX             ; Restore index

; Fork: Exclude if never recruited or dead Shadow
C385E9:  PLA             ; Eligible actor
         CMP $0000,Y     ; In actor slot?
         BNE C3860A      ; Try next if not
         JSR chk1EDX     ; Eligible char?
         BEQ C3861B      ; Skip if not

; Fork: Draw actor name
C38623:  REP #$20        ; 16-bit A
         LDA $E5         ; Name slot
         ASL A           ; Double it
         TAX             ; Index it
         LDA.L C38653,X  ; Text position
         TAY             ; Put in Y
         INC $E5         ; Name slot +1
         SEP #$20        ; 8-bit A
         JSR C334CF      ; Draw name

; Fork: Handle next eligible actor
C3861B:  PLX             ; List slot
         INX             ; List slot +1
         CPX #$0010      ; Done all 16?
         BNE C385DD      ; Loop if not
C38652:  RTS

; Draw all gear info except compatible actors
C38671:  JSR C36A41      ; Clear BG3 map B
         JSR txt2C       ; Color: Blue
         LDX #C38D45     ; Text ptrs loc
         LDA #$12        ; Strings: 18
         JSR C369BA      ; Draw stat names

; Fork: Draw non-evasion stat modifiers
C3868E:  JSR C38085      ; Color: User's
         JSR C3BFCB      ; Item in slot
         JSR C38321      ; Compute index
         PHX             ; Memorize it
         PHX             ; Memorize it
         PHX             ; Memorize it
         REP #$20        ; 16-bit A
;        TDC             ; Terminator
         STA $7E9E8D     ; Set mod B3+4
         LDA $D85010,X   ; Mod indices
         LDX #$0006      ; 1st: Vigor
.loop    PHX             ; Save pos index
         PHA             ; Save mod pack
         PHA             ; Save mod pack
         LDA.L modpos,X  ; Tilemap ptr
         TAY             ; Put in Y
         JSR C3BAB_      ; Set position
         REP #$20        ; 16-bit A
         PLA             ; Mod indices
         AND #$000F      ; Get current
         ASL A           ; Double it
         JSR C38836      ; Draw modifier
         REP #$20        ; 16-bit A
         PLA             ; Modifier pack
         JSR C3CC33      ; ÷16
         PLX             ; Pos index
         DEX             ; Pos index -1
         DEX             ; Pos index -1
         BPL .loop       ; Loop till < 0
         SEP #$20        ; 8-bit A

; Fork: Draw Evade and MBlock modifiers
C387EB:  LDA #$01        ; Modifiers: 2
         STA $E5         ; Save counter
         LDY #$8743      ; Evade's pos
         PLX             ; Item index
         LDA $D8501A,X   ; Evasion mods
.loop    PHA             ; Memorize them
         PHA             ; Memorize them
         JSR C3BAB_      ; Set position
;        TDC             ; Clear A
         PLA             ; Evasion mods
         AND #$0F        ; Get current
         ASL A           ; x2
         ASL A           ; x4
         TAX             ; Index it
         LDA.L C38854+2,X; Ones digit
         STA $7E9E8D     ; Add to string
         REP #$20        ; 16-bit A
         LDA.L C38854,X  ; Sign and tens
         JSR C3883_      ; Draw modifier
         LDY #$8843      ; MBlock's pos
         PLA             ; Evasion mods
         JSR C3CC33      ; ÷16
         DEC $E5         ; Done MBlock?
         BEQ .loop       ; Loop if not

; Fork: Draw defensive properties
C38703:  PLX             ; Item index
         JSR itmtyp_     ; Item's class
         DEC A           ; Weapon?
         BEQ C38746      ; Branch if so
         LDY #$86C3      ; Text position
         JSR C387B3      ; Draw Defense
         PLX             ; Item index
         PHX             ; Memorize it
         INX             ; Point to Mag.Def
         LDY #$87C3      ; Text position
         JSR C387B3      ; Draw Mag.Def
         JSR C38959      ; Draw elements
         PLX             ; Item index
         REP #$20        ; 16-bit A
         LDA $D85003,X   ; Spell and rate
         STA $F5         ; Memorize them
         SEP #$20        ; 8-bit A
         XBA             ; Rate = 0?
         BEQ C387EA      ; Skip if so
         LDY #$832F      ; Tilemap ptr
         JSR C3BAB_      ; Set position
         JSR C35AED      ; Draw string
C387EA:  LDX #C38D69     ; Text ptrs loc
         JMP C3322X      ; Draw elem types

; Fork: Draw offensive properties
C38746:  JSR C3879C      ; Draw Bat.Pwr
         PLX             ; Item index
         JSR itm_pr_     ; Allows Runic?
         PHA             ; Save properties
         PHA             ; Save properties
         BCC C3876E      ; Branch if not
         LDY #C38E30     ; Text pointer
         JSR draw20      ; Draw "Runic"
C3876E:  PLA             ; 2-hand OK?
         BPL C38781      ; Branch if not
         LDY #C38E38     ; Text pointer
         JSR draw20      ; Draw "2-hand"
C38781:  PLA             ; Properties
         AND #$04        ; Can SwdTech?
         BEQ C38794      ; Branch if not
         LDY #C38E26     ; Text pointer
         JSR draw20      ; Draw "SwdTech"
C38794:  LDY #C38E1D     ; Text pointer
         JSR draw2c      ; Draw "Attack"
         BRA C388A0      ; Draw elements

; Build and draw list of absorbed, nulled, and weak elements
C38959:  PEA $7BE9       ; Pos: Absorbed
         PEA $7CCD       ; Pos: Nulled
         PEA $7CE9       ; Pos: Weak pts
         LDA #$03        ; 1st: Weak pts
.elem    STA $E5         ; Save type
         LDX $4216       ; Item index
.index   INX             ; Index +1
         DEC A           ; Type -1
         BNE .index      ; Loop till 0
         LDA $D85015,X   ; Elements
         JSR C388AE      ; Build list
         PLX             ; Tilemap ptr
         JSR C388FE      ; Draw list
         LDA $E5         ; Types left
         DEC A           ; One less left
         BNE .elem       ; Loop till last

; Fork: Handle attack or halved elements
C388A0:  LDX $4216       ; Item index
         LDA $D8500F,X   ; Elements
         JSR C388AE      ; Build list
         LDX #$7BCD      ; Tilemap ptr

; Fork: Draw current list of elements
C388FE:  STX $EB         ; Set dest LBs
         LDA #$7E        ; Bank: 7E
         STA $ED         ; Set dest HB
         LDX $00         ; Index: 0
C38900:  TDC             ; Clear A
         LDA $7EAA8D,X   ; Element to draw
         BMI C38926      ; Exit if no more
         PHX             ; Save list index
         REP #$20        ; 16-bit A
         ASL A           ; Element x2
         ASL A           ; Element x4
         ADC #$3580      ; Add base attribs
         STA [$EB]       ; Set NW tile
         INC A           ; Tile num +1
         LDY #$0040      ; Map ptr: SW
         STA [$EB],Y     ; Set SW tile
         INC A           ; Tile num +1
         LDY #$0002      ; Map ptr: NE
         STA [$EB],Y     ; Set NE tile
         INC A           ; Tile num +1
         LDY #$0042      ; Map ptr: SE
         STA [$EB],Y     ; Set SE tile
         LDA $EB         ; Tilemap ptr
         ADC #$0004      ; Skip 2 tiles
         STA $EB         ; Save changes
         SEP #$20        ; 8-bit A
         PLX             ; List index
         INX             ; List index +1
         CPX #$0006      ; Done 6 entries?
         BNE C38900      ; Loop if not
C38926:  RTS

; Build current list of elements
C388AE:  JSR wmAA8D      ; WRAM: 7E/AA8D
         LDX #$FFFF      ; Current: Water
C388B9:  INX             ; Elem number +1
         ASL A           ; Draw current?
         BCC C388C3      ; Skip it if not
         PHA             ; Save elements
         TXA             ; Current element
         STA $2180       ; Add to list
         PLA             ; Elements left
C388C3:  BNE C388B9      ; Loop if any
C3C13B:  SEP #$20        ; 8-bit A
         LDA #$FF        ; Terminator
C3C13D:  STA $2180       ; End list
         RTS

; Draw weapon's Bat.Pwr in gear data menu
C3879C:  JSR C3BFCB      ; Item in slot
         CMP #$1C        ; Atma Weapon?
         BEQ C387C0      ; Hide if so
         CMP #$16        ; Soul Sabre?
         BEQ C387C0      ; Hide if so
         CMP #$51        ; Dice?
         BEQ C387C0      ; Hide if so
         CMP #$52        ; Fixed Dice?
         BEQ C387C0      ; Hide if so
         LDY #$8643      ; Text position
C387B3:  PHY             ; Push position
         JSR itm_pw_     ; Bat.Pwr
         JMP C304CY      ; Draw 3 digits

; Fork: Hide power
C387C0:  LDY #C38D71     ; Text pointer
         JMP C302F9      ; Draw "???"

; Draw a non-evasion stat modifier for gear data menu
C38836:  TAX             ; Modifier index
         LDA.L C38880,X  ; Sign and digit
C3883_:  STA $7E9E8B     ; Add to string
C38847:  SEP #$20        ; 8-bit A
         LDY #$9E89      ; 7E/9E89
         LDA #$7E        ; Source HB
         JMP C302FB      ; Draw modifier

; 07: Sustain non-shifted gear data menu
C38983:  LDA $08         ; Pushing A?
         BMI C3898F      ; Branch if so
         LDA $09         ; No-autofire keys
         BIT #$02        ; Pushing left?
         BEQ C389A8      ; Branch if not
C3898F:  JSR C30EB2      ; Sound: Click
         DEC $99         ; Disable arrow
         LDY #$5E65      ; Pan + Shifted menu
C3898_:  LDA #$0A        ; Frames: 10
         JMP C363FY      ; Init scroll data

; Fork: Handle B
C389A8:  BPL C38926      ; If not pushing B
         INC $26         ; Next: Choice 1
         DEC $99         ; Disable arrow
         LDX #$7B49      ; 7E/7B49
         LDY #$0500      ; Bytes: 1280
         JSR C3A79_      ; Clear BG3b
         JSR C324E8      ; Show cursor: Y
         JSR C3091F      ; Create scrollbar
         JSR C3B4E_      ; Description: On
         JSR C30F39      ; Queue BG3 upload
         JSR C31368      ; Upload it now
;        TDC             ; BG1 H-Shift: 0
         STA $7E9A10     ; Unhide item list
         JMP C30EA9      ; Sound: Cursor

; 5E: Sustain shifted gear data menu
C389E6:  LDA $09         ; No-autofire keys
         BIT #$81        ; Using B or right?
         BEQ C38AF_      ; Exit if not
C389F2:  JSR C30EA9      ; Sound: Cursor
         LDY #$0765      ; Pan + Screen 1
         JSR C3898_      ; Scroll timer: 10
         JSR C3241Y      ; Scroll speed: 12

; Fork: Create arrow
C38595:  STZ $99         ; Enable arrow
         LDY #C37A19     ; C3/7A19
         PEA $8008       ; Pos: 8, 128
         PEA $EC6F       ; Anim table ptr
C3859X:  JSR qanim1      ; Queue anim fn
C3859Y:  LDA #$D8        ; Anim ptr HB
         JMP C3EA0W      ; Set object data

; 2F: Initialize item usage menu
C38A84:  JSR C32A76      ; Init variables
         LDX #C38A3F     ; C3/8A3F
         JSR C30341      ; Draw quantity box
         JSR C38A47      ; Draw item info
         JSR C3319F      ; Upload tilemaps
         LDA #$30        ; Queue: Sustain menu
         JMP C32AA5      ; Next: Fade-in

; 30: Sustain item usage menu
C38A96:  JSR C30EFD      ; Queue stats upload
         JSR C30F61      ; And item info's
         LDA $08         ; Pushing A?
         BPL C38AAC      ; Branch if not
         JSR C32D0B      ; Target's address
         JSR C38C2B      ; Item
         CMP #$E7        ; Rename Card?
         BEQ C38AC0      ; Branch if so
         JSR ld_ail      ; Load status
         JSR C38B3D      ; Valid target?
         BCC C38B0A      ; Fail if not
         JSR C30EE5      ; Play heal sound
         JSR C38C2B      ; Selected item
         PHA             ; Memorize it
         PHA             ; Memorize it
         LDX #$0002      ; Mode: Item
         JSL $C20009     ; Get new status
         JSR st_ail      ; Set new status
         PLA             ; Selected item
         JSR C38C33      ; Heal HP & MP
         PLA             ; Selected item
         JSR C39D97      ; Destroy item
         JSR C38A6D      ; Redraw quantity
         JSR C32C01      ; Redraw party
         JSR ld28        ; X = Item slot
         LDA $1969,X     ; Item quantity
         BEQ C38AB5      ; Close menu if 0
C38AF_:  RTS

; Fork: Handle B
C38AAC:  LDA $09         ; Pushing B?
         BPL C38AF_      ; Exit if not
         JSR C30EA9      ; Sound: Cursor
C38AB5:  LDA #$42        ; Allow back row
         TRB $45         ; And cursor memory
         LDA #$12        ; Queue: Item menu
         JMP C3B4B8      ; Next: Fade-out

; Fork: Rename Card
C38AC0:  LDA $0000,Y     ; Actor
         CMP #$0E        ; Guest?
         BCS C38B0A      ; Fail if so
         STY $0206       ; Save actor adr
         LDA #$E7        ; Item number
         JSR C39D97      ; Destroy item
         LDA #$FE        ; Actor renaming
         STA $0205       ; Set next menu
         JMP C3AA9_      ; Sound + Fade-out
C38B0A:  JMP C3B66F      ; Buzzer + Mosaic

; Draw item info for item usage menu
C38A47:  LDY #C38E41     ; Text pointer
         JSR draw20      ; Draw "Owned:"
         LDY #$790B      ; Tilemap ptr
         JSR C3BAB_      ; Set position
         JSR C3BFCB      ; Y = Item slot
         JSR C3B26_      ; Draw item name

; Fork: Draw quantity owned
C38A6D:  JSR C38085      ; Color: User's
         JSR ld28        ; X = Item slot
         LDA $1969,X     ; Item quantity
         LDX #$7A93      ; Text position
         JMP C304B_      ; Draw 2 digits

; Determine whether item target is valid
C38B3D:  PHA             ; Memorize item
         LDA $F8         ; Target's status
         BMI C38B85      ; Branch if Wound
         TAX             ; Copy to X
         PLA             ; Item
         CMP #$F8        ; Green Cherry?
         BEQ C38BA9      ; Branch if so
         CMP #$FB        ; Echo Screen?
         BEQ C38BD0      ; Fail if so
         BCS C38BC4      ; If Dried Meat
         SBC #$EA        ; Potion type?
         BCC C38BC4      ; Branch if so
         SBC #$03        ; Tincture type?
         BCC C38BD5      ; Branch if so
         SBC #$02        ; Elixir?
         BCC C38BE5      ; Branch if so
         ASL A           ; Item x2
         TAX             ; Index it
         JMP (vlditem,X) ; Handle item

; Jump table for the above
vlditem: dw C38BD0       ; Fenix Down
         dw C38B8E       ; Revivify
         dw C38BBB       ; Antidote
         dw C38B97       ; Eyedrop
         dw C38BA0       ; Soft
         dw C38BB2       ; Remedy
         dw C38C11       ; Sleeping Bag

; Fork: Fenix Down
C38B85:  PLA             ; Item
         CMP #$F0        ; Fenix Down?
         BEQ C38BE3      ; Succeed if so
C38BD0:  CLC             ; Mark failure
         RTS

; Fork: Antidote
C38BBB:  LDA $F8         ; Status
         LSR A           ; Poison?
         BRA C38B91      ; Fail if not

; Fork: Revivify
C38B8E:  LDA $F8         ; Status
C38B91:  LSR A           ; Zombie?
         BRA C38B99      ; Fail if not

; Fork: Eyedrop
C38B97:  LDA $F8         ; Status
C38B99:  LSR A           ; Dark?
         RTS             ; Fail if not

; Fork: Green Cherry
C38BA9:  TXA             ; Status
         ASL A           ; Imp?
         BRA C38BA3      ; Fail if not

; Fork: Soft
C38BA0:  LDA $F8         ; Status
C38BA3:  ASL A           ; Shift bits
         ASL A           ; Petrify?
         RTS             ; Fail if not

; Fork: Remedy
C38BB2:  LDA $F8         ; Status
         AND #$65        ; Imp? Petrify?
C38BB7:  BEQ C38BD0      ; Fail if none
C38BE3:  SEC             ; Mark success
         RTS

; Fork: Dried Meat, Tonic, Potion, X-Potion
C38BC4:  TXA             ; Zombie? KO?
         AND #$C2        ; Or Petrify?
         BNE C38BD0      ; Fail if any
         BRA C32B9A      ; Or if full HP

; Fork: Tincture, Ether, X-Ether
C38BD5:  TXA             ; Zombie? KO?
         AND #$C2        ; Or Petrify?
         BNE C38BD0      ; Fail if any
         BRA C32BBC      ; Or if full MP

; Fork: Sleeping Bag
C38C11:  LDA $F8         ; Dark/Zom/Pois?
         AND #$F7        ; Clear/Imp/Pet/KO?
         BNE C38BE3      ; Succeed if any
         LDA $FB         ; Float status?
         BMI C38BE3      ; Succeed if so
;        BRA C38BE5      ; Test HP and MP

; Fork: Elixir
C38BE5:  TXA             ; Zombie? KO?
         AND #$C2        ; Or Petrify?
         BNE C38BD0      ; Fail if any
         JSR C32B9A      ; At full HP?
         BCS C38BE3      ; Succeed if so
         BRA C32BBC      ; Fail if full MP

; Get selected recovery item
C38C2B:  JSR ld28        ; X = Item slot
C38C2F:  LDA $1869,X     ; Item in slot
C38C32:  RTS

; Handle HP and MP recovery from item
C38C33:  JSR itm_pwr     ; Item's power
         STA $B2         ; Memorize it
         STZ $B3         ; Clear HB
         JSR itm_pr_     ; Fixed healing?
         PHA             ; Save properties
         BCS C38C76      ; Branch if not
         AND #$10        ; Affects HP?
         BEQ C38C5C      ; Branch if not

; Fork: Fixed HP healing
C38C4D:  REP #$21        ; 16-bit A; C-
         LDA $B2         ; Item's power
         JSR add_hp      ; Add to HP

; Fork: Fixed MP healing
C38C5C:  PLA             ; Properties
         AND #$20        ; Affects MP?
         BEQ C38D0F      ; Exit if not
         REP #$21        ; 16-bit A; C-
         LDA $B2         ; Item's power
         BRA C38CC1      ; Add to MP

; Fork: Fractional HP healing
C38C76:  AND #$10        ; Affects HP?
         BEQ C38CA0      ; Branch if not
         JSR boosthp     ; Define max HP
         JSR C38CD6      ; Max HP x Pwr/16
         JSR add_hp      ; Add to HP

; Fork: Fractional MP healing
C38CA0:  PLA             ; Properties
         AND #$20        ; Affects MP?
         BEQ C38C32      ; Exit if not
         JSR boostmp     ; Define max MP
         JSR C38CD6      ; Max MP x Pwr/16
C38CC1:  TYX             ; Target's address
         ADC $0D,X       ; Add target's MP
         STA $0D,X       ; Set target's MP

; Fork: Keep MP from exceeding max MP, set Carry if lower
C32BBC:  JSR boostmp     ; Define max MP
         PHY             ; Save index
         INY             ; Stat index +1
         INY             ; Stat index +1
         INY             ; Stat index +1
         INY             ; Stat index +1
         BRA C32BAA      ; Truncate MP

; Add heal amount to target's HP
add_hp:  TYX             ; Target's address
         ADC $09,X       ; Add target's HP
         STA $09,X       ; Set target's HP

; Fork: Keep HP from exceeding max HP, set Carry if lower
C32B9A:  JSR boosthp     ; Define max HP
         PHY             ; Push Y
C32BAA:  REP #$20        ; 16-bit A
         DEC A           ; Max HP -1
         CMP $0009,Y     ; Current < max?
         BCS C32BB9      ; Exit if so
         INC A           ; Max HP +1
         STA $0009,Y     ; Truncate HP
C32BB9:  SEP #$20        ; 8-bit A
         PLY             ; Restore index
         RTS

; Multiply max HP or MP by item's power, divide by 16
C38CD6:  PHY             ; Save Y
         TAY             ; Max HP or MP
         LDA $B2         ; Item's power
         JSR mul16       ; Power x max
         STA $EB         ; Set gain B3
         REP #$20        ; 16-bit A
         TXA             ; Product LBs
         LDX #$0004      ; Loop counter
.loop    LSR $EB         ; Halve HB
         ROR A           ; Halve LBs w/ C
         DEX             ; Counter -1
         BNE .loop       ; Loop till 0
         PLY             ; Restore Y
         CLC             ; Prepare ADC
C38D0F:  RTS

; Draw party gear overview
C38EED:  JSR bg1_1x2     ; BG1: 32x64 at $00
         JSR bg1b_cl     ; Clear BG1 maps
         JSR C36A28      ; Clear BG2 map A
         JSR C36A3C      ; Clear BG3 map A
         JSR mainbox     ; Draw window

; Fork: Draw all text
C38F1C:  JSR C38085      ; Color: User's
         LDX #$0003      ; Member slot: 4
C38F1_:  PHX             ; Save member slot
         TDC             ; Clear A
         LDA $69,X       ; Empty slot?
         BMI C38FC_      ; Skip if so
         LDA.L gear_y,X  ; Gear's Y pos
         PHA             ; Memorize it
         TXA             ; Member slot
         STA $28         ; Memorize it
         JSR C34352      ; Actor's address
         REP #$20        ; 16-bit A
         LDA.L gearchr,X ; Text position
         TAY             ; Put in Y
         JSR C334CF      ; Draw actor name

; Fork: Draw member's gear
C38F8A:  LDY $67         ; Actor's address
         LDX #$0005      ; 1st: R-Hand
C38F92:  PHX             ; Push gear slot
         PHY             ; Push actor adr
         PHY             ; Push actor adr
         STZ $E5         ; X-Pos HB: 0
         LDA.L C38FD5,X  ; X position
         STA $E4         ; Memorize it
         LDA.L C38FDB,X  ; Relative Y
         CLC             ; Prepare ADC
         ADC $07,S       ; Add base Y
         LDX $E4         ; X position
         JSR sk_slt3     ; Compute map ptr
         JSR C3941D      ; Test "Empty"
         LDA $05,S       ; Gear slot
         CMP #$04        ; Either hand?
         BCS C38FC0      ; Branch if so
         STZ $E0         ; Forbid "Empty"
C38FC0:  PLX             ; Actor's address
         LDA $1F,X       ; Item in slot
         JSR C39479      ; Draw item's name
         PLY             ; Actor's address
         INY             ; Reach next slot
         PLX             ; Gear slot
         DEX             ; Gear slot -1
         BPL C38F92      ; Loop till < 0
         PLA             ; Unstack base Y

; Fork: Handle next member
C38FC_:  PLX             ; Member slot
         DEX             ; Member slot -1
         BPL C38F1_      ; Loop till < 0
         JMP mvmapx4     ; Upload tilemaps

; Load equipped item's name
C38FE1:  CMP #$FF        ; Empty item?
         BNE C3C068      ; Branch if not
         LSR $E0         ; Allow "Empty"?
         BCS C3C068      ; Branch if so
         LDX.W #!itm_ns  ; Spaces: 13
C38FE_:  JMP C357F3      ; Draw 13 spaces

; Fork: Load item's name, terminate string
C3C068:  JSR C3C06_      ; Load name
C3C096:  STZ $2180       ; End string
         RTS

; Load owned item's name, add colon (Item, Colosseum, Sell)
C380B9:  JSR C3BFCF      ; Item in slot
         BEQ C380F6      ; Blank if none
         JSR C3C06_      ; Load item name
         LDA.B #!coln    ; Char: ":"
C380B_:  STA $2180       ; Add to string
         BRA C3C096      ; End string

; Fork: Blank item name, colon, and quantity
C380F6:  LDX.W #!itm_ns+3; Spaces: 16
         BRA C38FE_      ; Draw 16 spaces

; Draw Equip menu, create portrait, update status via gear
C39032:  JSR C39093      ; Do boxes; face
         LDX #C3A2AE     ; Text ptrs loc
;        JSR txt2C       ; Color: Blue
         JSR C39063      ; Draw Head, Body
         JSR C3911B      ; Draw info; status
         JSR C3904E      ; Draw top options
         JSR C30E6E      ; Upload BG3 A+B
C39E0F:  JSR C39E23      ; Queue BG3 upload
         JMP C30EFD      ; Queue BG1 upload

; Draw options in Equip menu
C3904E:  LDX #C3A2A6     ; Text ptrs loc
C39051:  JSR C38085      ; Color: User's
C39055:  LDA #$04        ; Strings: 4
         BRA C39066      ; Draw text

; Draw options in Relic menu
C3905C:  LDX #C3A2B2     ; Text ptrs loc
C3905F:  JSR C38085      ; Color: User's
C39063:  LDA #$02        ; Strings: 2
C39066:  JMP C369BA      ; Draw text

; Draw Relic menu, memorize original relics, create portrait
C3906A:  JSR C39093      ; Do boxes; face
;        JSR txt2C       ; Color: Blue
         LDX #C3A2B6     ; Text ptrs loc
         JSR C39063      ; Draw 2x "Relic"
         JSR C393F2      ; Define Y
         LDX $23,Y       ; Relics worn
         STX $B0         ; Save for Reequip
         JSR C39131      ; Draw info; status
         JSR C3905C      ; Draw menu options
         JSR C3A73D      ; Build desc map
         JMP C30E6E      ; Upload BG3 A+B

; Draw elements shared by Equip and Relic menus, create portrait
C39093:  JSR C32CDF      ; Load actor stats
         REP #$20        ; 16-bit A
         LDA #$0100      ; BG1 H-Shift: 256
         STA $7E9BD0     ; Hide gear list
         JSR bg1_2x1     ; BG1: 64x32 at $00
         LDA #$42        ; 32x64 at $4000
         STA $2109       ; Set BG3 map loc
         JSR C3960C      ; Reset BG2 X-Pos
         JSR bg1b_cl     ; Clear BG1 maps
         JSR bg2b_cl     ; Clear BG2 maps
         JSR bg3b_cl     ; Clear BG3 maps
         LDX #C3947F     ; C3/947F
         JSR C3034Y      ; Draw windows
         JSR C393F2      ; Actor's address
         JSR C38085      ; Color: User's
         LDY #$7BB7      ; Text position
         JSR C334CF      ; Draw actor name
         JSR C361B2      ; Create portrait
         JSR txt2C       ; Color: Blue
         LDX #C3A34D     ; Text ptrs loc
         LDA #$12        ; Strings: 18
         JSR C369BA      ; Draw stat names
         JMP mvmapx4     ; Upload tilemaps

; Draw actor info in Relic menu, update status based on gear
C39131:  JSR C38085      ; Color: User's
         JSR C39451      ; Draw relics

; Fork: Draw current stats, update status based on gear
C3913E:  JSR C32CDF      ; Load actor stats
         LDY $67         ; Actor's address
         TYX             ; Copy to X
         JSR C399E8      ; Set Bat.Pwr mode
         JSR C391C4      ; Update status
         JSR drwstt1     ; Draw stats
         LDX #$003F      ; Actor data index
C3920B:  LDA $11A0,X     ; Actor info
         STA $7E3000,X   ; Set as current
         DEX             ; Index -1
         BPL C3920B      ; Loop till < 0
         LDA $A0         ; 2-hand flag
         STA $A1         ; Set as current
         LDA $CD         ; Genji G. flag
         STA $CE         ; Set as current
         RTS

; Update status based on gear (Rage denotes Auto Float)
C391C4:  LDA $11D2       ; Gear immunity
         JSR C391EC      ; Deny ailments
         LDA $11D4       ; Gear auto status
         ORA $15,X       ; Add actor status
         LSR A           ; Auto Float or Rage?
         BCC C391EB      ; Exit if not
         LDA $11D4       ; Gear auto status
         AND #$01        ; Auto Float?
         BEQ C391E8      ; No status if so
         LDA #$81        ; Enable Rage, Float
C391E8:  STA $15,X       ; Set actor status
C391EB:  RTS

; Cure ailments blocked by gear
C391EC:  TYX             ; Actor's address
         AND #$25        ; Dark, Poison, Imp
         EOR #$FF        ; Toggle all bits
         AND $14,X       ; Retain actor's
         STA $14,X       ; Update status
         RTS

; Draw actor's basic stats in gear menu
drwstt1: LDY #C3913_     ; Coord tbl ptr
         PEA $7EB7       ; Pos for Bat.Pwr
         PEA $8037       ; Pos for Mag.Def
         PEA $7F37       ; Pos for Defense
         BRA drwstat     ; Branch below

; Draw actor's basic stats in Status menu
drwstt2: LDY #C3609_     ; Coord tbl ptr
         PEA $7E7D       ; Pos for Bat.Pwr
         PEA $7FFD       ; Pos for Mag.Def
         PEA $7EFD       ; Pos for Defense

; Draw actor's current stats, excluding Bat.Pwr
drwstat: JSR C30630      ; Set source
         JSR C38085      ; Color: User's
         LDY #$000A      ; 1st: MBlock
.loop    PHY             ; Save index
         REP #$20        ; 16-bit A
         LDA [$E7],Y     ; Text position
         PHA             ; Memorize it
         SEP #$20        ; 8-bit A
         LDA $11A0,Y     ; Actor stat
         PLX             ; Text position
         JSR C304CX      ; Draw 3 digits
         PLY             ; Stat index
         DEY             ; Stat index -1
         DEY             ; Stat index -1
         BPL .loop       ; Loop till < 0
         LDA $11BA       ; Defense
         PLX             ; Text position
         JSR C304CX      ; Draw 3 digits
         LDA $11BB       ; Mag.Def
         PLX             ; Text position
         JSR C304CX      ; Draw 3 digits
         JSR C39371      ; Define Bat.Pwr
         JSR C3052E      ; Turn into text
         PLX             ; Text position
         JMP C30486      ; Draw 3 digits

; Erase projected stats in Equip or Relic menu
C39C87:  LDX #$0405      ; Tile index
.row     LDY #$0006      ; Digits: 3
         TDC             ; Clear A
.digit   STA $7E7CBF,X   ; Clear tile data
         DEX             ; Tile index -1
         DEY             ; One less byte
         BNE .digit      ; Loop till last
         REP #$21        ; 16-bit A; C-
         TXA             ; Tile index
         SBC #$0079      ; Row -2, col +3
         TAX             ; Reindex it
         SEP #$20        ; 8-bit A
         BPL .row        ; Loop till last
         RTS

; Draw projected stats in Equip or Relic menu
C39233:  JSR ld_9D89     ; Empty item list?
         BEQ C39C87      ; Abort if so
         JSR C39A42      ; On a gray item?
         BCS C39C87      ; Abort if so
         JSR C39A4A      ; Actor's address
         LDA $25         ; Submenu
         LSR A           ; Equip menu?
         BCC C3924E      ; Branch if so
         INY             ; Point to L-Hand
         INY             ; Point to head
         INY             ; Point to body
         INY             ; Point to relic 1
C3924E:  LDA $001F,Y     ; Equipped item
         PHA             ; Preserve it
         JSR invitm_     ; Inventory item
         STA $001F,Y     ; Put on actor
         PHY             ; Save actor adr
         JSR C32CDF      ; Load actor data
         JSR C393F2      ; Base actor adr
         JSR C399E8      ; Set Bat.Pwr mode
         PLX             ; Actor's address
         PLA             ; Original item
         STA $1F,X       ; Restore gear
         LDX #$000A      ; 1st: MBlock
.loop    PHX             ; Save index
         REP #$20        ; 16-bit A
         LDA.L projsts,X ; Text position
         PHA             ; Memorize it
         SEP #$20        ; 8-bit A
         JSR C39320      ; Select palette
         LDA $11A0,X     ; Actor stat
         PLX             ; Text position
         JSR C304CX      ; Draw 3 digits
         PLX             ; Stat index
         DEX             ; Stat index -1
         DEX             ; Stat index -1
         BPL .loop       ; Loop till < 0
         LDX #$001A      ; Stat index
         JSR C39320      ; Select palette
         LDA $11BA       ; New Defense
         LDX #$7F3F      ; Text position
         JSR C304CX      ; Draw 3 digits
         LDX #$001B      ; Stat index
         JSR C39320      ; Select palette
         LDA $11BB       ; New Mag.Def
         LDX #$803F      ; Text position
         JSR C304CX      ; Draw 3 digits
         JSR C39371      ; Define Bat.Pwr
         JSR C3052E      ; Turn into text
         JSR C3934C      ; Select palette
         LDX #$7EBF      ; Text position
         JMP C30486      ; Draw 3 digits

; Select color for current projected stat in gear menu
C39320:  LDA $11A0,X     ; Projected stat
         CMP $7E3000,X   ; Matches old one?
         BEQ C3933C      ; Branch if so
         BCS C3935A      ; Yellow if higher
C39340:  JMP txt24       ; Color: Gray

; Select color for projected Bat.Pwr
C3934C:  JSR C39371      ; Set new Bat.Pwr
         JSR C393AB      ; Set old Bat.Pwr
         LDY $F3         ; New Bat.Pwr
         CPY $F1         ; Matches old?
         BEQ C3933C      ; Branch if so
         BCC C39340      ; Gray if less
C3935A:  JMP C3808A      ; Color: Yellow

; Fork: Use player's color
C3933C:  JMP C38085      ; Palette: 0

; Define current (Status) or projected (Equip, Relic) Bat.Pwr
C39371:  LDY $00         ; Stat index: 0
         TYX             ; Flag index: 0
         PEA $0002       ; Dest: $F3
         BRA C393A_      ; Branch below

; Define current Bat.Pwr for Equip or Relic menu
C393AB:  LDY #$1E60      ; Stat index
         LDX $00         ; Flag index: 0
         PHX             ; Dest: $F1
         INX             ; Flag index +1
C393A_:  PHB             ; Save DB
         JSR db7e        ; Set DB to 7E
         LDA $A0,X       ; 2-hand mode?
         BEQ C393C5      ; Branch if not
         LDA $11AC,Y     ; R-Hand's power
         BEQ C393BB      ; Branch if 0
         STA $11AD,Y     ; Set as L-Hand's
         BRA C393D4      ; Combine hands

; Fork: Two-handing L-Hand weapon
C393BB:  LDA $11AD,Y     ; L-Hand's power
         STA $11AC,Y     ; Set as R-Hand's
         BRA C393D4      ; Combine hands

; Fork: Not two-handing a weapon
C393C5:  LDA $CD,X       ; Got Genji Glove?
         BNE C393D4      ; Branch if so
         LDA $11AC,Y     ; R-Hand's power
         BEQ C393D4      ; Branch if 0
         TDC             ; Bat.Pwr: 0
         STA $11AD,Y     ; Set as L-Hand's

; Fork: Combine hands' Bat.Pwr
C393D4:  LDA $11AC,Y     ; R-Hand's power
         CLC             ; Prepare ADC
         ADC $11AD,Y     ; Add L-Hand's
         PLB             ; Restore DB
         PLX             ; Dest index
         STA $F1,X       ; Set Bat.Pwr LB
         TDC             ; Zero A
         ROL A           ; +1 if overflow
         STA $F2,X       ; Set Bat.Pwr HB
         RTS

; Check if "Empty" should be drawn for both hands
C3941D:  JSR C393F2      ; Actor's address
         STZ $E0         ; Forbid "Empty"
         LDA #$FF        ; Empty item
         CMP $1F,X       ; In R-Hand?
         BNE C39431      ; Forbid if not
         CMP $20,X       ; In L-Hand?
         BNE C39431      ; Forbid if not
         STA $E0         ; Allow "Empty"
C39431:  RTS

; Draw equipped relics in Relic menu
C39451:  LDY #$7B1B      ; Tilemap ptr
         JSR C3946D      ; Set Y, coords
         JSR C3945W      ; Draw relic 1
         LDY #$7B9B      ; Tilemap ptr
         JSR C3946D      ; Set Y, coords
         INY             ; Point to armor
C3945W:  INY             ; Point to relic 1
C3945X:  INY             ; Point to relic 2
C3945Y:  STZ $E0         ; Forbid "Empty"
C3945Z:  LDA $0021,Y     ; Relic 2
C39479:  JSR C38FE1      ; Load item name
         JMP C38847      ; Draw item name

; Set text position and load actor data address
C3946D:  JSR C3BAB_      ; Set position
C393F2:  JSR ld28x2      ; X = Member slot
         JSR C34354      ; Actor's address
         TYX             ; Copy to X
         RTS

; Set to condense text for party gear overview or final battle lineup
C39497:  LDY #C395D8     ; C3/95D8
C3949A:  LDA #$C3        ; Bank: C3
         STA $4354       ; Set src HB
         LDA #$0E        ; $210E
dma5a:   STA $4351       ; To BG1 V-Scroll
         STY $4352       ; Set src LBs
         LDA #$02        ; 1Rx2B to PPU
         JMP dma5b       ; Queue HDMA-5

; Set to condense text in Equip or Relic menu
C394B6:  LDX #$0066      ; Data index
C39522:  LDA.L C39571,X  ; V-Shift data
         STA $7E9849,X   ; Save in RAM
         DEX             ; Index -1
         BPL C39522      ; Loop till < 0
         LDX #$004E      ; Data index
C39530:  JSR topbg1      ; BG1 top row
         ADC.L C39583+1,X; Add V-Pan data
         STA $7E985C,X   ; Save in RAM
         SEP #$20        ; 8-bit A
         DEX             ; Index -1
         DEX             ; Index -1
         DEX             ; Index -1
         BPL C39530      ; Loop till < 0
         LDX #$000C      ; Data index
C394D9:  LDA.L C39564,X  ; BG1 H-Pan data
         STA $7E9BC9,X   ; Save in RAM
         DEX             ; Index -1
         BPL C394D9      ; Loop till < 0
         PEA $9BC9       ; BG1h src: 7E/9BC9
         LDY #C395D8     ; Src: C3/95D8
dma7b:   JSR C3C01B      ; Queue BG3 V-Pan
         PLY             ; BG1h source
         LDA #$7E        ; Src bank: 7E
         STA $4374       ; Set CH7 src HB
         JSR dma6a       ; Queue BG1 H-Pan
         LDY #$0E02      ; 1Rx2B to PPU
         STY $4370       ; To BG1 V-Scroll
         LDY #$9849      ; 7E/9849
         STY $4372       ; Set CH7 src LBs
         LDA #$80        ; Channel: 7
         TSB $43         ; Queue HDMA 7
         RTS

; 36: Handle Equip menu options
C39621:  JSR C3960C      ; Switch windows
         JSR C3904E      ; Draw options
         JSR C38E56      ; Handle D-Pad
         LDA $08         ; Pushing A?
         BPL C39635      ; Branch if not
         JSR C30EB2      ; Sound: Click
         JSR C32D0B      ; X = Cur slot x2
         JMP (C3966C,X)  ; Handle option

; Fork: Handle B
C39635:  LDA $09         ; Pushing B?
         BPL C39648      ; Branch if not
         JSR C32CDF      ; Update field FX
         JMP C3224D      ; SFX + Main menu

; Fork: Handle L and R, prepare for menu reset
C39648:  LDA #$35        ; Menu init command
         JMP C3201E      ; Handle L and R

; Draw blue "Equip" in Equip or Relic menu
C3964F:  LDY #C3A309     ; Text pointer
         BRA C39659      ; Draw title

; Draw blue "Remove" in Equip or Relic menu
C39656:  LDY #C3A311     ; Text pointer
C39659:  JMP C302F9      ; Draw title

; Erase options and title in Equip or Relic menu
C3965D:  LDY #$790D      ; Tilemap ptr
C3965_:  JSR C3BAB_      ; Set position
         LDX #$001C      ; Spaces: 28
         JMP C357F3      ; Erase text

; Jump table for Equip menu options
C3966C:  dw C39674       ; EQUIP
         dw C39685       ; OPTIMUM
         dw C3968E       ; RMOVE
         dw C3969F       ; EMPTY

; Handle "EQUIP" selection in Equip menu
C39674:  JSR C39614      ; Switch windows
         JSR C3964F      ; Draw blue "EQUIP"
         LDA #$56        ; Next: Slot choice
         BRA C3968_      ; Relocate cursor

; Handle "RMOVE" selection in Equip menu
C3968E:  JSR C39614      ; Switch windows
         JSR C39656      ; Draw blue "REMOVE"
         LDA #$55        ; C3/98CF
C3968_:  STA $26         ; Next: Slot choice
         JSR C315B8      ; Load navig data
         JMP C38E75      ; Relocate cursor

; Handle "OPTIMUM" selection in Equip menu
C39685:  JSR C396F0      ; Optimize gear
         BRA C3969_      ; Skip a line

; Handle "EMPTY" selection in Equip menu
C3969F:  JSR C396A8      ; Remove gear
C3969_:  STZ $4D         ; Cursor: "USE"

; Fork: Draw actor info, update status based on gear
C3911B:  JSR C39975      ; Draw hand names
         JSR C38085      ; Text pal: User's
         JSR C3913E      ; Do stats; status

; Fork: Draw equipped armaments
C393FC:  JSR C3941D      ; Test Empty x2
         LDY #$7A1B      ; Tilemap ptr
         JSR C3946D      ; Set Y, coords
         DEY             ; Actor adr -1
         JSR C393F_      ; Draw R-Hand item
         JSR C3941D      ; Test Empty x2
         LDY #$7A9B      ; Tilemap ptr
         JSR C3946D      ; Set Y, coords
C393F_:  DEY             ; Point to L-Hand
         JSR C3945Z      ; Draw L-Hand item

; Fork: Draw equipped helmet and armor
C39435:  LDY #$7B1B      ; Tilemap ptr
         JSR C3946D      ; Set Y, coords
         JSR C3945Y      ; Draw helmet
         LDY #$7B9B      ; Tilemap ptr
         JSR C3946D      ; Set Y, coords
         JMP C3945X      ; Draw armor

; Remove character's equipment
C396A8:  JSR C393F2      ; Actor's address
         LDY #$0004      ; Slots left: 4
.loop    LDA $1F,X       ; Item in slot
         JSR C39D5E      ; Put in stock
;        LDA #$FF        ; Empty item
         STA $1F,X       ; Clear slot
         INX             ; Gear slot +1
         DEY             ; One less slot
         BNE .loop       ; Loop till last
         RTS

; Optimize member's equipment (event command)
C396D2:  JSR C36915      ; Get party info
         LDX #$0003      ; Member slot: 4
         LDA $0201       ; Actor to equip
C396DA:  CMP $69,X       ; In member slot?
         BEQ C396E6      ; Branch if so
         DEX             ; Member slot -1
         BPL C396DA      ; Loop till < 0
C396E4:  STP             ; Freeze game...
C396E6:  TXA             ; Member slot
         STA $28         ; Memorize it
         JSR C396F0      ; Optimize gear
         RTL

; Optimize character's equipment
C396F0:  JSR C32CDF      ; Get gear FX
         JSR C396A8      ; Remove gear
         LDY $67         ; Actor's address
         STY $F3         ; Memorize it
         DEY             ; Decrease it
         DEY             ; Decrease it
         PHY             ; Push on stack
         JSR C39795      ; Get valid arms
         JSR C3A150      ; Sort by power
         LDA $11D8       ; Gear effects
         AND #$08        ; Gauntlet?
         BEQ C3971A      ; Branch if not

; Fork: Put a two-handed weapon in R-Hand
C39702:  JSR C3983F      ; Pick 2H weapon
         PLY             ; Actor's address
         JSR C3976Z      ; Set R-Hand item
         BRA C39780      ; Handle armor

; Fork: Put a weapon in R-Hand
C3971A:  PLY             ; Actor's address
         JSR C3976Y      ; Choose a weapon
         LDA $11D8       ; Gear effects
         AND #$10        ; Genji Glove?
         BNE C39751      ; Branch if so

; Fork: Put a shield in L-Hand
C39737:  JSR C397D7      ; Get valid items
         BRA C39754      ; Skip a line

; Fork: Put a weapon in L-Hand
C39751:  JSR C39795      ; Get valid items
C39754:  JSR C3A150      ; Sort by power
         LDY $F3         ; Actor's address
         DEY             ; Point to L-Hand
         JSR C3976Y      ; Choose a weapon

; Fork: Optimize armor
C39780:  INC $F3         ; Point to armor
         LDA #$03        ; Target: Torso
         JSR C3976X      ; Choose armor

; Fork: Optimize helmet
C3976B:  DEC $F3         ; Point to helmet
         LDA #$02        ; Target: Head
C3976X:  STA $4B         ; Set body part
         JSR C39B59      ; Get valid items
         JSR C3A150      ; Sort by defense
         LDY $F3         ; Actor's address
C3976Y:  JSR C39819      ; Choose helmet
C3976Z:  STA $0021,Y     ; Put on actor
         JMP C39D97      ; Adjust stock

; Compile compatible weapons for Optimum
C39795:  LDA #$01        ; Target: Weapons
         BRA C397DX      ; Skip a line

; Compile compatible shields for Optimum
C397D7:  LDA #$03        ; Target: Shields
C397DX:  STA $E9         ; Set sought class
         STA $EA         ; Alt class: None
C397DY:  JSR C39C2A      ; Init list
         JSR C39C41      ; Define compat
         LDY $00         ; Item slot: 1
C397E0:  JSR C3BFCF      ; Item in slot
         BEQ C3980A      ; Skip if none
         JSR itmtype     ; Item's class
         CMP $E9         ; Sought type A?
         BEQ C397F8      ; Branch if so
         CMP $EA         ; Sought type B?
         BNE C3980A      ; Skip if not
C397F8:  JSR C3BCF_      ; Actor can use?
         BEQ C3980A      ; Skip if not
         TYA             ; Item slot
         STA $2180       ; Add to list
         INC $E0         ; List size +1
C3980A:  INY             ; Item slot +1
         CPY #$00FF      ; Done 255?
         BNE C397E0      ; Loop if not
         LDA $E0         ; List size
C39814:  STA $7E9D89     ; Save to list
         RTS

; Compile compatible gear for actor's body part
C39B59:  LDA $4B         ; Body part
         CMP #$02        ; Either hand?
         BCC C39B72      ; Branch if so
         BEQ C39BB2      ; Branch if head

; Fork: Armor list
C39BEE:  DEC A           ; Seek: Armor
         BRA C397DX      ; Build list

; Fork: Weapons and shields
C39B72:  LDX #$0103      ; Seek: Weapons
         STX $E9         ; Seek: Shields
         BRA C397DY      ; Build list

; Fork: Helmet list
C39BB2:  ASL A           ; Seek: Helmets
         BRA C397DX      ; Build list

; Select first non-excluded item in sorted list of compatible gear
C39819:  PHY             ; Save Y
         TDC             ; Clear A
         LDY #$FFFF      ; RAM index: -1
C39821:  INY             ; RAM index +1
         LDX #$0009      ; ROM index
C39823:  PHX             ; Save index
         TYX             ; RAM index
         JSR invitem     ; Item in slot
         PLX             ; ROM index
         CMP $ED82E4,X   ; Excluded?
         BEQ C39821      ; Skip if so
         DEX             ; ROM index -1
         BPL C39823      ; Loop till < 0
         PLY             ; Restore Y
C3983B:  RTS

; Exclusions: Cursed Shld, Thornlet, Imp set, Atma Weapon, Drainer, Soul Sabre, Heal Rod


; Select first two-handed weapon from sorted list of compatible gear
C3983F:  JSR ld_9D89     ; List size
         DEC A           ; Empty list?
         BMI C3983B      ; Exit if so
         STA $E7         ; Set counter
         TDC             ; Clear A
         TAY             ; RAM index: 0
C3984B:  LDX #$0009      ; ROM index
C3984D:  PHX             ; Save index
         TYX             ; RAM index
         JSR invitem     ; Item in slot
         PLX             ; ROM index
         CMP $ED82E4,X   ; Excluded?
         BEQ C39878      ; Skip if so
         DEX             ; ROM index -1
         BPL C3984D      ; Loop till < 0
         PHA             ; Save item
         JSR itm_pro     ; Properties
         ASL A           ; 2-hand OK?
         PLA             ; Item
         BCS C3983B      ; Retain if so

; Fork: Try next weapon, ignore Gauntlet if no valid options
C39878:  INY             ; RAM index +1
         DEC $E7         ; Counter -1
         BPL C3984B      ; Loop till < 0
         BRA C39819      ; Pick 1H weapon

; 56: Handle selection of gear slot to fill
C39884:  JSR C38E72      ; Handle D-Pad
         JSR C39975      ; Recolor hand names
         LDA $08         ; Pushing A?
         BPL C398B4      ; Branch if not
         JSR C30EB2      ; Sound: Click
         LDA $4E         ; Cursor row
         STA $5F         ; Set body slot
         LDX #$5557      ; Next: Item list..
         STX $26         ; ..Return if empty
         JSR C39B59      ; Build item list
         JSR C3A150      ; Sort it by power
         JSR C39AEB      ; Cursor & Scrollbar
;        JSR C39233      ; Draw stat preview
         JSR C36A15      ; Blank item list
         JSR C31368      ; Refresh screen

; Fork: Draw and reveal list of available gear
C39CAC:  JSR C383F7      ; Define $E5, $E6
         JSR ld_9D89     ; Empty list?
         BEQ C39CDD      ; Abort if so
         CMP #$09        ; Over 8 items?
         BCC C39CC1      ; Branch if not
         LDA #$09        ; Rows left: 9
C39CC1:  PHA             ; Memorize it
         JSR C39CE2      ; Draw item name
         INC $E5         ; List slot +1
         JSR e6add2      ; BG1 WR row +2
         PLA             ; Rows left
         DEC A           ; One less left
         BNE C39CC1      ; Loop till last
         TDC             ; BG1 H-Shift: 0
C39CD6:  STA $7E9BD1     ; Unhide gear list
         RTS             ; Exit
C39CDD:  LDA $27         ; Old menu mode
         STA $26         ; Set as current
C39CE1:  RTS

; Fork: Handle B, L, and R
C398B4:  LDA #$60        ; Reset cmd for L/R
         BRA C398FX      ; Branch below

; 55: Handle manual gear removal
C398CF:  JSR C38E72      ; Handle D-Pad
         LDA $08         ; Pushing A?
         BPL C398F4      ; Branch if not
         JSR C30EB2      ; Sound: Click
         JSR C393F2      ; Actor's address
         REP #$21        ; 16-bit A; C-
         TYA             ; Move it to A
         SEP #$20        ; 8-bit A
         ADC $4B         ; Add cursor slot
         TAX             ; Index sum
         LDA $1F,X       ; Item in slot
         JSR C39D5E      ; Put in stock
;        LDA #$FF        ; Empty item
         STA $1F,X       ; Clear gear slot
         JSR C3911B      ; Redo text, status

; Fork: Handle B
C398F4:  LDA #$61        ; Reset cmd for L/R
C398FX:  LDX $08         ; Pushing B?
         BPL C39908      ; Branch if not
C398FA:  JSR C3B323      ; Relocate cursor
         LDA #$36        ; C3/9621
C398F_:  STA $26         ; Next: Option list
         JMP C30EA9      ; Sound: Cursor

; Fork: Handle L and R, prepare for menu reset
C39908:  JMP C3201E      ; Handle L and R

; 57: Handle gear browsing
C3990F:  JSR C39AD3      ; Handle navigation
         JSR C39233      ; Draw stat preview
         LDA $08         ; Pushing A?
         BPL C39944      ; Branch if not
         JSR C39A42      ; On a gray item?
         BCS C3996E      ; Fail if so
         LDA $001F,Y     ; Item to unequip
         JSR unequip     ; Put in stock; SFX
         STA $001F,Y     ; Equip chosen item
         JSR C39D97      ; Adjust stock
         JSR C3911B      ; Redo text, status
         BRA C3994D      ; Exit gear list

; Fork: Handle B
C39944:  LDA $09         ; Pushing B?
         BPL C39CE1      ; Exit if not
         JSR C30EA9      ; Sound: Cursor
C3994D:  JSR C315B8      ; Load navig data
         JSR ret_eqp     ; Clear stats, etc.
         JMP C38E75      ; Relocate cursor

; Fork: Invalid selection
C3996E:  JMP C3B66F      ; Buzzer + Mosaic

; Draw "R-hand" and "L-hand" in Equip menu
C39975:  LDA $11D8       ; Gear effects
         AND #$08        ; Gauntlet?
         BEQ C3998F      ; Blue x2 if not
         JSR C393F2      ; Actor's address
         LDA $1F,X       ; R-Hand item
         CMP #$FF        ; None?
         LDA $20,X       ; L-Hand item
         BCS C399A6      ; If R-Hand empty
         INC A           ; L-Hand empty?
         BNE C3998F      ; Blue x2 if not

; Fork: Check if two-handed weapon in R-Hand
C399CA:  PEA $242C       ; R=Blue, L=Gray
         LDA $1F,X       ; R-Hand item
         BRA C399A_      ; Test 2-handed

; Fork: Check if L-Hand also empty
C399A6:  CMP #$FF        ; L-Hand empty?
         BEQ C3998F      ; Blue x2 if so

; Fork: Check if two-handed weapon in L-Hand
C399AF:  PEA $2C24       ; R=Gray, L=Blue
C399A_:  JSR itm_pro     ; 2-hand OK?
         BMI C3998_      ; Branch if so
         PLX             ; Discard colors

; Fork: Draw both hands in blue
C3998F:  PEA $2C2C       ; Both = Blue
C3998_:  PLA             ; R-Hand color
         LDY #C3A2BA     ; Text pointer
         JSR C3999F      ; Draw "R-hand"
         LDY #C3A2C3     ; Text pointer
         PLA             ; L-Hand color
C3999F:  JMP C302F_      ; Draw "L-hand"

; Define Bat.Pwr mode (normal, doubled, or combined hands)
C399E8:  STZ $CD         ; Genji mode off
         LDA $11D8       ; Gear effects
         BIT #$10        ; Genji Glove?
         BEQ C399F3      ; Branch if not
         INC $CD         ; Pwr: Merge hands
C399F3:  STZ $A0         ; Power x2: Off
         BIT #$08        ; Gauntlet?
         BEQ C39A41      ; Exit if not
         LDA $1F,X       ; R-Hand item
         CMP #$FF        ; None?
         LDA $20,X       ; L-Hand item
         BCS C39A0E      ; If R-Hand empty
         INC A           ; L-Hand empty?
         BNE C39A41      ; Exit if not

; Fork: Check if two-handed weapon in R-Hand
C39A2B:  LDA $1F,X       ; R-Hand item
C39A2_:  JSR itm_pro     ; 2-hand OK?
         BPL C39A41      ; Exit if not
         INC $A0         ; Pwr x2: On
C39A41:  RTS

; Fork: Check if L-Hand also empty
C39A0E:  CMP #$FF        ; L-Hand empty?
         BNE C39A2_      ; Branch if not
         RTS

; Load address to selected body slot
C39A4A:  JSR C393F2      ; Actor's address
         LDA $5F         ; Body slot
.loop    INY             ; Address +1
         DEC A           ; Body slot -1
         BPL .loop       ; Loop till < 0
         DEY             ; Rectify address
         TDC             ; Clear A
         RTS

; Check if pointing at grayed out gear
C39A42:  JSR C39A4A      ; Actor's address
         LDA $4B         ; Cursor slot

; Determine whether gear list entry is disabled
C39A56:  TAX             ; Gear list slot
         JSR invitem     ; Item in slot
         JSR itmtype     ; Item's class
         STA $F6         ; Memorize it
         TYX             ; Actor's address
         LDA $5F         ; Body slot
         DEC A           ; L-Hand/relic 2?
         BEQ C39AA5      ; Branch if so
         BPL C39ACF      ; If head or body

; Fork: For R-Hand or relic 1
C39A79:  INX             ; Adjust index
         INX             ; Off-hand: Left

; Fork: For L-Hand or relic 2
C39AA5:  LDA $1E,X       ; Off-hand's item
C39AA8:  CMP #$FF        ; None?
         BEQ C39ACF      ; Enable if so
         JSR itmtype     ; Item's class
         STA $F7         ; Memorize it
         CMP #$03        ; Shield?
         BEQ C39AC5      ; Branch if so
         LDA $11D8       ; Gear effects
         AND #$10        ; Genji Glove?
         BNE C39ACF      ; Enable if so

; Fork: Prevent two shields or weapons
C39AC5:  LDA $F7         ; Held item's class
         CMP $F6         ; Like list item?
         BEQ C39AD0      ; Disable if so
C39ACF:  CLC             ; Set to enable
C39AD0:  RTS

; Handle D-Pad plus L and R for gear browsing
C39AD3:  JSR ld_9D89     ; List size
         CMP #$0A        ; Over 9 items?
         BCS C39ADE      ; Branch if so
         JSR C3072D      ; Handle D-Pad
C38EA0:  LDY #C38EAB     ; C3/8EAB
         JMP C30640      ; Relocate cursor

; Fork: Scrollable list
C39ADE:  LDA #$05        ; List type: Gear
         JSR C31F64      ; Handle L and R
         BCS C39AD0      ; Exit if pushed
         JSR C381C7      ; Handle D-Pad
C38E91:  LDY #C38EAB     ; C3/8EAB
         JMP C30648      ; Relocate cursor

; Initialize cursor data for list of available gear
C39AEB:  JSR ld_9D89     ; Number of items
         BEQ C39AD0      ; Exit if none
         PHA             ; Memorize it
         JSR C39DC4      ; Duplicate cursor
         STZ $4A         ; List scroll: 0
         JSR C33858      ; Load navig data
         PLA             ; Number of items
         CMP #$0A        ; Over 9?
         BCS C39B06      ; Branch if so
         STA $54         ; Set cursor limit
         BRA C38EA0      ; Relocate cursor

; Fork: Scrollable list
C39B06:  SBC #$09        ; Count - Page size
         PHA             ; Memorize result
         PHA             ; Do it again
         JSR C30933      ; Create scrollbar
         LDA #$60        ; Y: 96
         JSR C3BC0Y      ; Set scrollbar's
         TXY             ; Save queue index
         XBA             ; Bar range: 60 px
         TAX             ; Set dividend
         PLA             ; Item count - 9
         JSR divide      ; Range / (Count - 9)
         REP #$20        ; 16-bit A
         TXA             ; Quotient
         TYX             ; Queue index
         STA $7E354A,X   ; Set bar's V-Spd
         STZ $4F         ; List col/row: 1
         SEP #$20        ; 8-bit A
         JSR C38E91      ; Relocate cursor
         PLA             ; Item count - 9
         LDY #$0109      ; Cursor slots: 1x9
         JMP C3597Z      ; Scroll limit = A

; Initialize list of compatible gear
C39C2A:  LDX #$0008      ; List index
         LDA #$FF        ; Item slot 256
C39C2E:  STA $7E9D8A,X   ; Add to list
         DEX             ; List index -1
         BPL C39C2E      ; Loop till < 0
         STZ $E0         ; List size: 0
wm9D8A:  LDX #$9D8A      ; 7E/9D8A
         JMP wm_set      ; Set WRAM LBs

; Defaulting to item slot 256, which is unused in the SNES versions, provides a fallback
; for Optimum in case the list is empty or contains only banned items such as Cursed Shld.


; Define gear compatibility for actor
C39C41:  JSR C393F2      ; Actor's address
         LDA $00,X       ; Actor
C39C48:  ASL A           ; Double it
         TAX             ; Index it
         REP #$20        ; 16-bit A
         LDA.L C39C67,X  ; Compat bit
         STA $E7         ; Memorize it
         SEP #$20        ; 8-bit A
         LDA $11D8       ; Gear effects
         AND #$20        ; Get Merit Award
         ASL A           ; Put in bit 6
         ASL A           ; Put in bit 7
         TSB $E8         ; Add to compat
         RTS

; Bitfield for actor slots
C39C67:  dw $0001        ; Terra
         dw $0002        ; Locke
         dw $0004        ; Cyan
         dw $0008        ; Shadow
         dw $0010        ; Edgar
         dw $0020        ; Sabin
         dw $0040        ; Celes
         dw $0080        ; Strago
         dw $0100        ; Relm
         dw $0200        ; Setzer
         dw $0400        ; Mog
         dw $0800        ; Gau
         dw $1000        ; Gogo
         dw $2000        ; Umaro
         dw $4000        ; Guest 1
         dw $8000        ; Guest 2

; Draw name of item from list of available gear
C39CE2:  JSR C39A4A      ; Actor's address
         JSR C38085      ; Color: User's
;        TDC             ; Clear A
         LDA $E5         ; Gear list slot
         JSR C39A56      ; Gray out item?
         BCC C39CF1      ; Branch if not
         JSR C3808A      ; Color: Gray
C39CF1:  LDX #$0002      ; X: 2
         JSR sk_slt1     ; Compute map ptr
         JSR invitem     ; Item in slot
         CMP #$FF        ; None?
         BEQ C39D96      ; Exit if so
         JMP C3B26_      ; Draw item name

; Add item to inventory
C39D5E:  PHY             ; Save Y
         CMP #$FF        ; Empty item?
         BEQ C39D95      ; Exit if so
         LDY #$00FF      ; Item slot: 256
C39D63:  CMP $1869,Y     ; Slot holds it?
         BEQ C39D8A      ; Qty +1 if so
         DEY             ; Item slot -1
         BPL C39D63      ; Loop till < 0
         PHA             ; Save item

; Fork: Find an empty slot
C39D72:  INY             ; Item slot +1
         JSR C3BFCF      ; Empty slot?
         BNE C39D72      ; Loop if not

; Fork: Put item in empty slot
C39D7E:  PLA             ; Item to add
         STA $1869,Y     ; Put in slot
         LDA #$01        ; Quantity: 1
         BRA C39D92      ; Assign to slot

; Fork: Quantity +1
C39D8A:  LDA $1969,Y     ; Quantity owned
         CMP #$63        ; 99?
         BEQ C39D95      ; Exit if so
         INC A           ; Quantity +1
C39D92:  STA $1969,Y     ; Save changes
C39D95:  PLY             ; Restore Y
C39D96:  LDA #$FF        ; Empty item
         RTS

; Remove item from inventory
C39D97:  LDX #$0100      ; Slot: 256 (+1)
C39D9C:  DEX             ; Item slot -1
         CMP $1869,X     ; Slot holds it?
         BNE C39D9C      ; Loop if not
         DEC $1969,X     ; Quantity -1
         BNE C39DC3      ; Exit if not 0
         LDA #$FF        ; Empty item
         STA $1869,X     ; Put in slot
C39DC3:  RTS

; Duplicate cursor before browsing gear
C39DC4:  LDY #C39DDD     ; C3/9DDD
         JSR qanim2      ; Queue OAM fn
         PEI ($55)       ; Cursor's pos
         JMP C3EA0X      ; Set as dupe's

; Update cursor indicating selected gear slot
C39DDD:  BNE C39E00      ; If mode 1

; Mode 0: Initialize cursor
C39DE5:  LDA #$01        ; Dupe: Sustain
         TSB $46         ; Set menu flag
         LDY #C3088D     ; Anim table ptr
         JSR initob1     ; Set pose timer

; Mode 1: Sustain cursor
C39E00:  LDA $46         ; Menu flags
         LSR A           ; Remove dupe?
         BCC C39DC3      ; Delete if so
         JMP C30860      ; Set OAM; requeue

; Set to upload text tilemaps for Relic menu
C39E14:  JSR C30EFD      ; Queue BG1 upload
         LDA $021E       ; Frame counter
         LSR A           ; Odd value?
         BCS C39E23      ; Favor stats if so

; Set to upload VWF description, freeze CGRAM
C3A991:  LDA #$01        ; CGRAM: Freeze
         TRB $45         ; Set NMI flag
         LDY #$6800      ; VRAM: $6800
         PEA $0700       ; Bytes: 1792
         JMP C3A60X      ; Src: 7E/A271

; Set to upload BG3 tilemaps A and upper B
C39E23:  LDY #$4000      ; VRAM: $4000
C39E2_:  LDX #$7849      ; Src: 7E/7849
         PEA $0880       ; Bytes: 2176
         JMP C3A60Y      ; Set variables

; 58: Initialize Relic menu
C39E4B:  JSR C39E50      ; Init variables
         JSR C3906A      ; Draw menu, etc.
         LDA #$59        ; C3/9EB8
         JMP C32AA5      ; Queue: Option list

; 59: Handle Relic menu options
C39EB8:  JSR C39E23      ; Queue BG3 upload
         JSR C3960C      ; Switch windows
         JSR C3905C      ; Draw options
         JSR C38EC3      ; Handle D-Pad
         LDA $08         ; Pushing A?
         BPL C39ED0      ; Branch if not
         JSR C30EB2      ; Sound: Click
         JSR C32D0B      ; X = Cur slot x2
         JMP (C39FD7,X)  ; Handle option

; Fork: Handle B
C39ED0:  LDA $09         ; Pushing B?
         BPL C39EDC      ; Branch if not
         JSR C30EA9      ; Sound: Cursor
         JSR C39EEB      ; Handle Reequip

; Fork: Handle L and R, prepare for menu reset
C39EDC:  LDA #$58        ; Menu init command
         JMP C3201E      ; Handle L and R

; Test Reequip activation, select menu commands accordingly
C39EEB:  JSR C32CDF      ; Get gear effects
         JSR C393F2      ; Actor's address
         LDA $00,X       ; Actor
         CMP #$0D        ; Umaro?
         BEQ C39EFF      ; Branch if so
         LDX $23,Y       ; Current relics
         CPX $B0         ; Unchanged?
         BEQ C39EFF      ; Branch if so

; Fork: Compare old and new relics
C39F6F:  LDX #$0002      ; 1st: M.Award
.loop    LDA.L reequip,X ; Reequip item
         CMP $0023,Y     ; Relic 1?
         BEQ C39F06      ; Trigger if so
         CMP $0024,Y     ; Relic 2?
         BEQ C39F06      ; Trigger if so
         CMP $B0         ; Old relic 1?
         BEQ C39F06      ; Trigger if so
         CMP $B1         ; Old relic 2?
         BEQ C39F06      ; Trigger if so
         DEX             ; Item index -1
         BPL .loop       ; Loop till < 0
C39EFF:  LDA #$04        ; Queue main menu
         CLC             ; Reequip: No
         JMP C3B4B8      ; Next: Fade-out

; Fork: Triggered Reequip
C39F06:  LDA #$06        ; Main cursor: Off
         TRB $46         ; Set menu flag
         JSR C39E0F      ; Queue text upload
         JSR C3965D      ; Erase top text
         LDY #C3A40F     ; Text pointer
         JSR C302F9      ; Draw warning
         LDA #$40        ; C3/1C1A
         STA $27         ; Queue optimizing
         LDX $00         ; Ptr index: 0
         LDA $1D4E       ; Game options
         AND #$10        ; Optimum mode?
         BEQ .optim      ; Branch if so
         INC $27         ; Queue removal
         INX             ; Ptr index +1
         INX             ; Ptr index +1
         INX             ; Ptr index +1
         INX             ; Ptr index +1
.optim   LDA $26         ; Menu mode
         CMP #$59        ; Relic options?
         BEQ .high       ; Branch if so
         INX             ; Ptr index +1
         INX             ; Ptr index +1
.high    REP #$20        ; 16-bit A
         LDA.L reeqptr,X ; Text pointer
         TAY             ; Put it in Y
         JSR C302F9      ; Draw Empty/Opti
         LDA #$F0        ; Frames: 240
         STA $22         ; Set fade delay
         LDA #$3F        ; C3/9FB1
         STA $26         ; Next: Late fade
         SEC             ; Reequip: Yes
         RTS

; Relics that trigger Reequip
reequip: db $D0          ; Gauntlet
         db $D1          ; Genji Glove
         db $DA          ; Merit Award

; 3F: Handle delayed fade-out for Reequip
C39FB1:  LDA $22         ; Delay expired?
         BEQ C39FCC      ; Fade if so
         DEC $22         ; Timer -1
         LDX $08         ; Pushing B?
         BMI C39FCC      ; Fade if so
         TXA             ; No-autofire keys
         BIT #$30        ; Pushing L or R?
         BEQ C39FCE      ; Exit if not
C39FCC:  STZ $26         ; Next: Fade-out
C39FCE:  RTS

; Jump table for Relic menu options
C39FD7:  dw C39FDB       ; EQUIP
         dw C39FEC       ; REMOVE

; Handle "EQUIP" selection in Relic menu
C39FDB:  JSR C39614      ; Switch windows
         JSR C3964F      ; Draw blue "EQUIP"
         LDA #$5A        ; Next: Slot list
         BRA C39FE_      ; Relocate cursor

; Handle "REMOVE" selection in Relic menu
C39FEC:  JSR C39614      ; Switch windows
         JSR C39656      ; Draw blue "REMOVE"
         LDA #$5C        ; C3/A10A
C39FE_:  STA $26         ; Next: Slot list
         JSR C315D4      ; Load navig data
         JMP C38EDE      ; Relocate cursor

; 5A: Handle selection of relic slot to fill
C39FFD:  JSR C39E14      ; Queue text upload
         JSR C38EDB      ; Handle D-Pad
         LDA $08         ; Pushing A?
         BPL C3A033      ; Branch if not
         JSR C30EB2      ; Sound: Click
         LDA $4E         ; Cursor row
         STA $5F         ; Set body slot
         LDX #$5A5B      ; Next: Item list..
         STX $26         ; ..Return if empty
         LDA #$05        ; Seek: Relics
         JSR C397DX      ; Get valid items
         JSR C3A150      ; Sort by Defense
         JSR C39AEB      ; Cursor & Scrollbar
         JSR C36A15      ; Clear BG1 map A
;        JSR C31368      ; Refresh screen
         JSR C39CAC      ; Draw item list
         JSR C39233      ; Draw stat preview
         JSR C39E23      ; Queue BG3 upload
         JMP C31368      ; Refresh screen

; Fork: Handle B
C3A033:  LDA $09         ; Pushing B?
         BMI C3A13_      ; Branch if so

; Fork: Handle L and R, prepare for menu reset
C3A047:  LDA #$62        ; Menu init command
         BRA C3A14D      ; Handle L and R

; 5C: Handle relic removal
C3A10A:  JSR C39E23      ; Queue BG3 upload
         JSR C38EDB      ; Handle D-Pad
         LDA $08         ; Pushing A?
         BPL C3A132      ; Branch if not
         JSR C30EB2      ; Sound: Click
         LDA $4B         ; Cursor slot
         STA $5F         ; Copy for C3/9A4A
         JSR C39A4A      ; Actor's address
         TYX             ; Put it in X
         LDA $23,X       ; Relic worn
         JSR C39D5E      ; Put in stock
;        LDA #$FF        ; Empty item
         STA $23,X       ; Clear relic worn
         JSR C39131      ; Draw info; status

; Fork: Handle B
C3A132:  LDA $09         ; Pushing B?
         BPL C3A146      ; Branch if not
C3A13_:  JSR C38EBD      ; Relocate cursor
         LDA #$59        ; Next: Option list
         JMP C398F_      ; Sound: Cursor

; Fork: Handle L and R, prepare for menu reset
C3A146:  LDA #$63        ; Menu init command
C3A14D:  JMP C3201E      ; Handle L and R

; 5B: Handle relic browsing
C3A097:  JSR C3B4E_      ; Description: On
         JSR C39E14      ; Queue text upload
         JSR C39AD3      ; Handle navigation
         JSR C39233      ; Draw stat preview
         JSR C3A1D8      ; Load description

; Fork: Handle A
C3A0A7:  LDA $08         ; Pushing A?
         BPL C3A0DC      ; Branch if not
         JSR C39A4A      ; Actor's address
         LDA $0023,Y     ; Relic to unequip
         JSR unequip     ; Put in stock; SFX
         STA $0023,Y     ; Equip chosen item
         JSR C39D97      ; Reduce stock
         JSR C39131      ; Draw info; status
         BRA C3A0E5      ; Exit list

; Fork: Handle B
C3A0DC:  LDA $09         ; Pushing B?
         BPL C3A1D7      ; Exit if not
         JSR C30EA9      ; Sound: Cursor
C3A0E5:  JSR C38590      ; Reset/Stop desc
         JSR C315D4      ; Load navig data
         JSR ret_eqp     ; Clear stats, etc.
         JMP C38EDE      ; Relocate cursor

; Return from selecting an item to equip
ret_eqp: JSR C39C87      ; Clear stat preview
         LDA #$C1        ; Top cursor: Off
         TRB $46         ; Scrollbar: Off
         LDA $5F         ; Former position
         STA $4E         ; Set cursor row
         DEC $26         ; Menu mode -1
         LDA #$01        ; BG1 H-Shift: 256
         JMP C39CD6      ; Hide gear list

; Sort compatible gear by power after loading power ratings
C3A150:  LDX #$AC8D      ; 7E/AC8D
         STX $2181       ; Set WRAM LBs
         TDC             ; Clear A
         TAX             ; Gear slot: 1
         JSR ld_9D89     ; List size
         CMP #$02        ; 0 or 1?
         BCC C3A1D7      ; Exit if so
         TAY             ; Copy to Y
         STY $E7         ; Memorize it
C3A167:  PHX             ; Save gear slot
         JSR invitem     ; Item in slot
         JSR itm_pwr     ; Item's power
         STA $2180       ; Add to list
         TDC             ; Clear A
         PLX             ; Gear slot
         INX             ; Gear slot +1
         CPX $E7         ; End of list?
         BNE C3A167      ; Loop if not

; Fork: Sort list of compatible gear by power
C3A187:  JSR db7e_       ; Set DB to 7E
         DEC $E7         ; List size -1
         LDY $E7         ; List size
C3A190:  LDX $00         ; List slot: 1
C3A192:  LDA $AC8D,X     ; Item's power
         CMP $AC8E,X     ; Next is worse?
         BCS C3A1B7      ; Skip if not
         PHA             ; Memorize it
         LDA $9D8A,X     ; Item number A
         PHA             ; Memorize it
         LDA $AC8E,X     ; Item power B
         STA $AC8D,X     ; Replace A's
         LDA $9D8B,X     ; Item number B
         STA $9D8A,X     ; Replace A's
         PLA             ; Item number A
         STA $9D8B,X     ; Replace B's
         PLA             ; Item power A
         STA $AC8E,X     ; Replace B's
C3A1B7:  INX             ; List slot +1
         CPX $E7         ; End of list?
         BNE C3A192      ; Loop if not
         DEY             ; Items left -1
         BNE C3A190      ; Loop till 0
         PLB             ; Restore DB
C3A1D7:  RTS

; Reset autofire delays for "A" in battle
C3A424:  LDX #$0320      ; Initial: 32
         STX $0225       ; Repeat: 3
         RTL

; Decode joypads for battle
C3A445:  TAX             ; Member slot
         LDY $4218       ; Player 1 keys
         LDA $1D54       ; Two players?
         BPL C3A462      ; Branch if not
         LDA $1D4F       ; Assigned pad
         AND.L C3A53D,X  ; Player 1's?
         BEQ C3A462      ; Branch if so
         LDY $421A       ; Player 2 keys
C3A462:  JSR C3A483      ; Decode keys
         JSR C3A4BD      ; Do autofire
         JSR C3A4F6      ; Adjust it
         RTL

; Decode joypads for field
C3A47A:  JSR C3A48_      ; Decode P1 keys
         RTL

; Update non-time-based joypad keys
C3A48_:  LDY $4218       ; Player 1 keys
C3A483:  PHY             ; Save pad's keys
         REP #$20        ; 16-bit A
         TYA             ; Pushed keys
         AND #$2000      ; Get Select
         XBA             ; Put in low byte
         LSR A           ; Convert to R key
         ORA $01,S       ; Add rest of keys
         AND #$DFF0      ; Clear pad type
         STA $06         ; Set fast keys
         PLA             ; Unstack keys
         LDA $0C         ; Former keys
         TAX             ; Save them in X
         EOR #$FFFF      ; Invert them
         AND $06         ; Keep current
         STA $08         ; Set no-auto keys
         LDY $06         ; Pushed keys
         STY $0C         ; Set former keys
         LDA $4218       ; Player 1 keys
         ORA $421A       ; Add player 2's
         STA $04         ; Set merged keys
         SEP #$20        ; 8-bit A
         RTS

; Update slow autofire keys
C3A4BD:  CPX $06         ; Any keys changed?
         BNE C3A4E5      ; Retain new if so
         LDA $0227       ; Initial delay
         BEQ C3A4D4      ; Branch if over
         DEC $0227       ; Initial delay -1
         BNE C3A4F1      ; No keys if > 0
C3A4D4:  DEC $0228       ; Repeat delay -1
         BNE C3A4F1      ; No keys if > 0
         JSR C3A4EB      ; Repeat delay: 3
         TXY             ; Pushed keys
         BRA C3A4F3      ; Set slow keys
C3A4E5:  LDA #$08        ; Delay: 8
         STA $0227       ; Set initial
C3A4EB:  LDA #$03        ; Delay: 3
         STA $0228       ; Set repeat timer
C3A4F1:  LDY $08         ; No-autofire keys
C3A4F3:  STY $0A         ; Set slow keys
C3A4F5:  RTS

; Adjust slow autofire keys for battle
C3A4F6:  LDA $06         ; Pushing A?
         BPL C3A517      ; Branch if not
         LDA $0225       ; Initial delay
         BEQ C3A506      ; Branch if over
         DEC $0225       ; Initial delay -1
         BNE C3A522      ; New keys if > 0
C3A506:  DEC $0226       ; Repeat delay -1
         BNE C3A522      ; New keys if > 0
         LDA #$03        ; Delay: 3
         STA $0226       ; Reset repeat timer
         LDA #$80        ; Button: A
         TSB $0A         ; Add to slow keys
         RTS

; Fork: Not pushing A
C3A517:  JSL C3A424      ; Reset timers
C3A522:  LDA $08         ; No-autofire keys
         STA $0A         ; Set slow keys
C3A526:  RTS

; Bitfield for joypad assignments
C3A53D:  db $01          ; Member A
         db $02          ; Member B
         db $04          ; Member C
         db $08          ; Member D

; Build description tilemap for Item, Relic, or Colosseum menu
C3A73D:  LDY #$79CD      ; Dest: 7E/79CD
C3A740:  LDX $00         ; Source index

; Build tilemap for description or ending pseudonym
C3A783:  STY $EB         ; Set map adr LBs
         LDA #$7E        ; Bank: 7E
         STA $ED         ; Set map adr HB
         REP #$20        ; 16-bit A
         LDA.L dsctbl,X  ; Column count
         STA $E7         ; Memorize it
.row     LDY $00         ; Column index: 0
         LDA.L dsctbl+2,X; Row's base tile
         BEQ .exit       ; Exit if null
.col     STA [$EB],Y     ; Put in tilemap
         INC A           ; Tile +1
         INC A           ; Tile +1
         INY             ; Column index +1
         INY             ; Column index +1
         CPY $E7         ; End of row?
         BNE .col        ; Loop if not
         INX             ; ROM index +1
         INX             ; ROM index +1
         LDA $EB         ; Tilemap ptr
         ADC #$003F      ; Go 1 row down
         STA $EB         ; Save changes
         BRA .row        ; Do next row
.exit    SEP #$20        ; 8-bit A
         RTS

; Base tile attributes per row for descriptions
dsctbl:  dw $0038        ; Width: 28 tiles
         dw $3500        ; Tile 256, pal 5
         dw $3501        ; Tile 257, pal 5
         dw $3538        ; Tile 312, pal 5
         dw $3539        ; Tile 313, pal 5
         dw $0000        ; End

; Base tile attributes per row for ending pseudonyms
         dw $0018        ; Width: 12 tiles
         dw $2410        ; Tile 16, pal 1
         dw $2411        ; Tile 17, pal 1
         dw $0000        ; End

; Update VWF description
C3A80E:  CMP #$02        ; Mode 1?
         BEQ C3A82F      ; Branch if so
         BCS C3A81B      ; If mode 2

; Mode 0: Clear description
C3A818:  JSR C3A796      ; Clear desc GFX

; Mode 2: Initialize variables
C3A81B:  STZ $8D         ; Line width: 0
         LDX $2D         ; Queue index
         JSR C382D6      ; Next: Mode 1
         REP #$20        ; 16-bit A
         STZ $33CA,X     ; Char index: 0
         STZ $344A,X     ; VRAM index: 0
         SEP #$20        ; 8-bit A

; Mode 1: Sustain description
C3A82F:  LDA $26         ; Menu mode
         CMP #$17        ; Item menu top?
         BEQ C3A88D      ; Blank if so
         LDA $46         ; Menu flags
         AND #$C0        ; No scrollbar?
         BEQ C3A841      ; Branch if so
         LDA $06         ; Pushed keys
         AND #$30        ; Using L or R?
         BNE C3A895      ; Reset if so
C3A841:  LDA $45         ; Menu flags
         BIT #$20        ; Ignore D-Pad?
         BNE C3A84D      ; Branch if so
         LDA $07         ; Pushed keys
         AND #$0F        ; Using D-Pad?
         BNE C3A895      ; Reset if so
C3A84D:  LDA $45         ; Menu flags
         BIT #$10        ; Clear desc?
         BNE C3A895      ; Reset if so
         TXY             ; Queue index
         LDX $344A,Y     ; VRAM index
         STX $ED         ; Memorize it
         LDX $33CA,Y     ; Char index

; Fork: Handle line break
C3A85D:  LDA $9EC9,X     ; Text character
         BEQ C3A89C      ; If terminator
         CMP #$01        ; Line break?
         BNE C3A875      ; Branch if not
         STZ $8D         ; Line width: 0
         REP #$20        ; 16-bit A
         LDA #$0380      ; Base tile: 57
         STA $344A,Y     ; Set VRAM index
         BRA C3A87D      ; Queue upload

; Fork: Handle character
C3A875:  JSR C3A8B1      ; Load graphics

; Fork: Queue upload, advance char index
C3A87D:  INX             ; Char index +1
         LDY $2D         ; Queue index
         REP #$20        ; 16-bit A
         TXA             ; Char index
         STA $33CA,Y     ; Put in queue
         SEP #$20        ; 8-bit A
         JSR C3A991      ; Queue upload
         SEC             ; Set to requeue
         RTS

; Fork: Clear description, stop updating
C3A88D:  JSR C3A796      ; Clear desc GFX
         JSR C3A991      ; Set to upload
         CLC             ; Set to dequeue
         RTS

; Fork: Set to reset description
C3A895:  STZ $3649,X     ; Next: Mode 0
         SEC             ; Set to requeue
         RTS

; Fork: Handle terminator
C3A89C:  TYX             ; Queue index
         INC $3649,X     ; Next: Mode 2
         SEC             ; Set to requeue
C3A89_:  LDA #$01        ; CGRAM: Refresh
         TSB $45         ; Set NMI flag
         RTS

; Load VWF character's graphics into string, add shadow effect
C3A8B1:  PHX             ; Save X
         AND #$7F        ; Adjust char
         PHA             ; Save text char
         XBA             ; Put it in B
         LDA #$0B        ; Rows left: 11
         STA $E5         ; Set counter
         ASL A           ; GFX data size
         JSR mul8        ; Char x size
         TAY             ; Set ROM index
         PHB             ; Save DB
         JSR db7e        ; Set DB to 7E
;        TDC             ; Clear A
         LDA $8D         ; Textline width
         AND #$F8        ; Drop WIP tile's
         REP #$20        ; 16-bit A
         ASL A           ; x2
         ASL A           ; x4
         ADC $ED         ; Add VRAM index
         TAX             ; Set RAM index

; Fork: Load 1bpp row graphics, shift to fit string
C3A8D9:  REP #$20        ; 16-bit A
         PHX             ; Save RAM index
         TYX             ; ROM index
         LDA $C490C0,X   ; 12-px row GFX
         STZ $E7         ; Blank slice C
         STZ $E9         ; Clear byte 4
         STA $E8         ; Set slices A+B

; Fork: Shift row for string insertion
C3A94F:  LDA $8D         ; Line width
         AND #$0007      ; WIP tile's
         CMP #$0004      ; Center of tile?
         BEQ C3A8EA      ; Abort if so
         AND #$0003      ; Shift amount
         BCS .mv_r       ; If rightward
         EOR #$0003      ; Invert amount
.mv_l    ASL $E7         ; Do slice B+C
         ROL $E9         ; Shift slice A
         DEC A           ; Counter -1
         BPL .mv_l       ; Loop till < 0
         BRA C3A8EA      ; Add to string

; Fork: Shift character row right
.mv_r    LSR $E9         ; Shift slice A
         ROR $E7         ; Do slice B+C
         DEC A           ; Counter -1
         BNE .mv_r       ; Loop till 0

; Fork: Insert row segment C, copy as shadow below (2bpp)
C3A8EA:  PLX             ; RAM index
         LDA $E7         ; Slices B and C
         SEP #$20        ; 8-bit A
         ORA $A2B9,X     ; Add WIP plane A
         STA $A2B9,X     ; Set WIP plane A
         REP #$20        ; 16-bit A
         LSR A           ; Shift 1 px right
         SEP #$20        ; 8-bit A
         ORA $A2BC,X     ; Add WIP plane B
         STA $A2BC,X     ; Set WIP plane B

; Fork: Insert row segment B, copy as shadow below (2bpp)
C3A904:  REP #$20        ; 16-bit A
         LDA $E8         ; Slices A and B
         SEP #$20        ; 8-bit A
         ORA $A299,X     ; Add WIP plane A
         STA $A299,X     ; Set WIP plane A
         REP #$20        ; 16-bit A
         LSR A           ; Shift 1 px right
         SEP #$20        ; 8-bit A
         ORA $A29C,X     ; Add WIP plane B
         STA $A29C,X     ; Set WIP plane B

; Fork: Insert row segment A, copy as shadow below (2bpp)
C3A91F:  LDA $E9         ; Slice A
         ORA $A279,X     ; Add WIP plane A
         STA $A279,X     ; Set WIP plane A
         LSR A           ; Shift 1 px right
         ORA $A27C,X     ; Add WIP plane B
         STA $A27C,X     ; Set WIP plane B

; Fork: Handle next row
C3A937:  INX             ; RAM index +1
         INX             ; RAM index +1
         INY             ; ROM index +1
         INY             ; ROM index +1
         DEC $E5         ; One less row
         BNE C3A8D9      ; Loop till last

; Fork: Add character's width to line total
C3A93F:  PLB             ; Restore DB
         TDC             ; Clear A
         PLA             ; Text character
         TAX             ; Index it
         LDA $8D         ; Line width
         CLC             ; Prepare ADC
         ADC $C48FE0,X   ; Add char width
         STA $8D         ; Save changes
         PLX             ; Restore X
         RTS

; Load and upload VWF graphics for all elements
C3A9A9:  LDY #$6C00      ; VRAM: $6C00
         LDX $00         ; Element index
C3A9C0:  LDA $D8E90E,X   ; Elem text char
         BEQ C3A9E2      ; If terminator
         PHX             ; Save index
         PHY             ; Save VRAM ptr
         PHY             ; Save VRAM ptr
         PHA             ; Save text char
         LDY #$0040      ; Tiles: 4
         JSR clrvwf2     ; Clear GFX buffer
         PLA             ; Elem text char
         JSR C3A8B1      ; Load graphics
         PLY             ; VRAM pointer
         JSR C3A600      ; Set src and dst
         LDA #$01        ; CGRAM: Freeze
         TRB $45         ; Set NMI flag
         JSR C314AC      ; Upload graphics
         REP #$21        ; 16-bit A; C-
         PLA             ; VRAM pointer
         ADC #$0020      ; Skip 4 tiles
         TAY             ; Put in Y
         SEP #$20        ; 8-bit A
         PLX             ; Elem index
         INX             ; Elem index +1
         BRA C3A9C0      ; Do next element
C3A9E2:  JMP C30F89      ; Dequeue upload

; Clear VWF graphics buffer, init variables
clrvwf2: JSR C3A79X      ; Clear GFX buffer
         STZ $8D         ; Line width: 0
         LDX $00         ; Letter index: 0
         STX $ED         ; VRAM index: 0
         RTS

; 45: Initialize final battle lineup
C3A9F8:  JSR C3352X      ; Reset/Stop stuff
         LDY #C3AC63     ; C3/AC63
         JSR C305FE      ; Load navig data
         JSR C3AC5D      ; Relocate cursor
         JSR C3AAFD      ; Reset user's list
         JSR bg1_1x2     ; BG1: 32x64 at $00
         JSR mainbox     ; Draw window
         JSR bg1b_cl     ; Clear BG1 maps
         JSR C36A3C      ; Clear BG3 map A
         JSR C38085      ; Color: User's
         LDX #C3AC84     ; Text ptrs loc
         JSR drawx3      ; Draw End, etc.
         JSR C3AC19      ; Draw numbers
         JSR C3AB4D      ; Draw candidates
         JSR mvmapx4     ; Upload tilemaps
         JSR C39497      ; Set to shift text
         JSR C30EFD      ; Queue text upload
         LDA #$46        ; Queue: Sustain menu
         JMP C346A_      ; Queue cursor OAM

; 46: Sustain final battle lineup
C3AA25:  JSR C3AB89      ; Redraw candidates

; Fork: Redraw player's actor list
C3ABB4:  JSR C38085      ; Color: User's
         LDY #$3FB1      ; Tilemap ptr
         STY $F5         ; Set position
         LDX #$000B      ; List slot: 12
C3ABBF:  JSR C3351_      ; Set pos, WRAM
         PHX             ; Save list slot
         LDA $0205,X     ; Actor in slot
         JSR C3ABF0      ; Draw actor name
         PLX             ; List slot
         DEX             ; List slot -1
         BPL C3ABBF      ; Loop till < 0

; Fork: Handle D-Pad and B
C3AA31:  JSR C3AC5A      ; Handle D-Pad
;        TDC             ; Clear A
         LDA $4A         ; R-List's size
         TAY             ; Put it in Y
         LDA $09         ; Pushing B?
         BPL C3AA63      ; Branch if not
         JSR C30EA9      ; Sound: Cursor
         TYA             ; R-List's empty?
         BEQ C3AA62      ; Exit if so
         DEC A           ; R-List size -1
         STA $4A         ; Save changes
         TAX             ; Index it
         LDA $0205,X     ; Last R-List actor
         PHA             ; Memorize it
         LDA #$FF        ; Actor: None
         STA $0205,X     ; Clear R-List slot
         LDX #$000C      ; L-List slot: 12+1
         PLA             ; Removed actor
C3AA50:  DEX             ; L-List slot -1
         CMP $7E9D8A,X   ; In L-List slot?
         BNE C3AA50      ; Loop if not
C3AA5C:  LDA #$20        ; Color: User's
C3AA5E:  STA $7EAA8D,X   ; Set L-List name's
C3AA62:  RTS

; Fork: Handle actor selection
C3AA63:  BIT #$10        ; Pushing Start?
         BNE C3AA99      ; Branch if so
         LDA $08         ; Pushing A?
         BPL C3AAB1      ; Exit if not
         JSR C30EB2      ; Sound: Click
         LDA $4B         ; On Reset?
         BEQ C3AAA7      ; Branch if so
         CMP #$0D        ; On "End"?
         BEQ C3AA99      ; Branch if so
         JSR sk_slt5     ; Clicked actor
         BMI C3AAB1      ; Exit if nobody
         LDA $7EAA8C,X   ; Name color
         CMP #$20        ; Player's?
         BNE C3AAB1      ; Exit if not
         LDA #$28        ; Color: Gray
         STA $7EAA8C,X   ; Set L-Name's
         JSR sk_slt6     ; Chosen actor
         STA $0205,Y     ; To user's list
         INC $4A         ; R-List size +1
         CPY #$000B      ; Added 12 actors?
         BNE C3AAB1      ; Branch if not
         LDA #$0D        ; Row: "End"
         STA $4E         ; Set cursor's
C3AAB1:  RTS

; Fork: Handle "End"
C3AA99:  TYA             ; R-List size
         CMP #$0C        ; 12 actors?
         BEQ C3AA9_      ; Branch if so
         LDX $00         ; L-List slot: 1
C3AAD2:  LDA $7EAA8D,X   ; Name color
         CMP #$20        ; Player's?
         BNE C3AAE7      ; Skip if not
         LDA $7E9D8A,X   ; L-List actor
         STA $0205,Y     ; Add to list
         INY             ; R-List slot +1
C3AAE7:  INX             ; L-List slot +1
         CPX #$000C      ; Done all 12?
         BNE C3AAD2      ; Loop if not
C3AA9_:  LDA #$FF        ; Queue menu exit
         JMP C31E3A      ; Sound + Fade-out

; Fork: Handle "Reset"
C3AAA7:  INC $4E         ; Move cursor down
C3AAFD:  LDX #$000B      ; List slot: 12
C3AAFF:  LDA #$FF        ; Actor: None
         STA $0205,X     ; Put in R-Slot
         JSR C3AA5C      ; Lit L-List name
         DEX             ; List slot -1
         BPL C3AAFF      ; Loop if not
         STZ $4A         ; R-List size: 0
         RTS

; Compile and draw candidates for final battle
C3AB4D:  LDX #$000F      ; List slot
C3AAF0:  JSR st_9D8_     ; Clear slot
         DEX             ; List slot -1
         BPL C3AAF0      ; Loop till < 0

; Fork: Compile candidates
         INX             ; 1st: Terra
         TXY             ; List slot: 1
C3AB51:  PHX             ; Save actor
         LDA $1850,X     ; Party info
         ASL A           ; Object's off?
         BPL C3AB81      ; Skip if so
         AND #$0E        ; In a party?
         BEQ C3AB81      ; Skip if not
         REP #$20        ; 16-bit A
         TXA             ; Actor slot
         JSR charadr     ; Actor's address
         LDA $00,X       ; Actor in slot
         CMP #$0E        ; Guest?
         BCS C3AB81      ; Skip if so
         TYX             ; List slot
         LDA $01,S       ; Actor slot
         STA $7E9D8A,X   ; Add to list
         INY             ; List slot +1
C3AB81:  PLX             ; Actor slot
         INX             ; Actor slot +1
         CPX #$0010      ; Done all 16?
         BNE C3AB51      ; Loop if not

; Fork: Draw list of candidates
C3AB89:  LDY #$3F95      ; Tilemap ptr
         STY $F5         ; Set position
         LDX #$000C      ; List slot: 12
C3AB90:  JSR C3351_      ; Set pos, WRAM
         PHX             ; Save list slot
         LDA $7EAA8C,X   ; Name palette
         STA $29         ; Set attributes
         JSR sk_slt6     ; Actor in slot
         JSR C3ABF0      ; Draw actor name
         PLX             ; List slot
         DEX             ; List slot -1
         BNE C3AB90      ; Loop till 0
         RTS

; Draw actor's name for final battle lineup
C3ABF0:  ASL A           ; Actor x2
         BMI .blank      ; Blank if none
         JSR charadX     ; Actor's address
         TXY             ; Copy to Y
         JSR C334D2      ; Draw name
         BRA C3ABFE      ; Skip 2 lines
.blank   LDX #$0006      ; Spaces: 6
         JSR C357F3      ; Blank slot
C3ABFE:  REP #$21        ; 16-bit A; C-
         LDA $F5         ; Tilemap ptr
         SBC #$007F      ; Go 2 rows up
         STA $F5         ; Save changes
         SEP #$20        ; 8-bit A
         RTS

; Draw both lists of numbers for final battle lineup
C3AC19:  LDX #$3F8F      ; Tilemap ptr
         JSR C3AC3B      ; Draw left list
         LDX #$3FAB      ; Tilemap ptr
C3AC3B:  STX $F5         ; Set position
         LDA #$0C        ; Number: 12
C3AC3D:  PHA             ; Save number
         LDX $F5         ; Text position
         JSR C304B_      ; Draw 2 digits
         JSR C3ABFE      ; Go 2 rows up
         PLA             ; Number
         DEC A           ; Number -1
         BNE C3AC3D      ; Loop if > 0
         RTS

; 31: Initialize Colosseum item menu
C3ACAA:  STZ $0201       ; Face Shadow: No
         JSR C31AE2      ; Init list vars
         JSR C37D25      ; Relocate cursor
         JSR bg1_2x1     ; BG1: 64x32 at $00
         LDX #C3AD8A     ; C3/AD8A
         JSR C3034Y      ; Draw windows
         JSR C36A19      ; Clear BG1 map B
         JSR bg3d_cl     ; Clear BG3 maps
         LDY #C3AD9A     ; Text pointer
         JSR draw2c      ; Draw "Colosseum"
         JSR C3A73D      ; Build desc map
         JSR C37E0D      ; Draw inventory
         LDY #C3ADA6     ; Text pointer
         JSR draw20      ; Draw "Select..."
         JSR mvmapx5     ; Upload tilemaps
         JSR C382F1      ; Load description
         JSR C31B0E      ; Shift & OBJ & Fade
         INC $45         ; Unfreeze CGRAM
         JSR C31368      ; Refresh screen
         LDA #$02        ; Main cursor: On
         STA $46         ; Bar/Blinker: Off
         JSR C307B0      ; Queue cursor OAM
         LDA #$1E        ; C3/ACDC
         JMP C31C15      ; Queue: Sustain menu

; 1E: Sustain Colosseum item menu
C3ACDC:  JSR itm_shr     ; Handle D-Pad, etc.
         BCS C3B22B      ; Exit if L or R

; Fork: Handle A
C3ACF0:  LDA $08         ; Pushing A?
         BPL C3AD14      ; Branch if not
         JSR C3BFCB      ; Selected item
         BEQ C3AD0E      ; Fail if nothing
         STA $0205       ; Set item bet
         LDA #$4E        ; Queue: Matchup
         JMP C31E3A      ; Sound + Fade-out

; Fork: Invalid selection
C3AD0E:  JSR C3B66F      ; Buzzer + Mosaic

; Fork: Handle B
C3AD14:  LDA $09         ; Pushing B?
         BPL C3B22B      ; Exit if not
         JSL C31514      ; Clear item bet
         JMP C3224F      ; SFX + Menu exit

; 4E: Initialize Colosseum matchup menu
C3ADB7:  JSR C3352Y      ; Reset/Stop stuff
         LDA #$FB        ; Maintain Win1 HDMA
         TRB $43         ; Disable the rest
         LDA #$02        ; Gradient set: 2
         JSL $D4CA1D     ; Create gradient
         JSR C36CC7      ; Load actor colors
         JSR C3AF46      ; Prize/enemy data
         JSR C3B34B      ; Load arena
         JSR C36C84      ; Cursor/VS colors
         JSR C3B323      ; Load navig data
         JSR C3B32C      ; Relocate cursor
         JSR C3AE07      ; Queue "VS" OAM
         JSR C318D1      ; Set to shift text
         JSR C3AE34      ; Draw menu; members
         JSR C3A89_      ; Unfreeze CGRAM
         LDY #C3B2A5     ; C3/B2A5
         JSR qanim1      ; Queue challenger fn
;        JSR C30EFD      ; Queue enemy upload
         LDA #$4F        ; Queue: Sustain menu
         JSR C346A_      ; Queue cursor OAM
         JMP C30F39      ; Queue text upload

; Initialize Colosseum actor sprite, excluding enemy Shadow
C3B1F3:  JSR gfxset1     ; Graphics set
C3B20E:  PHB             ; Save DB
         JSR C378FA      ; Set anim ptr
         JSR D8_35CA     ; Ptr HB: D8
         LDA $E1         ; X position
         STA $33CA,Y     ; Set sprite's
         LDA $E2         ; Y position
         STA $344A,Y     ; Set sprite's
         PLB             ; Restore DB
C3B22B:  RTS

; Create "VS" symbol
C3AE07:  LDY #C3B2F3     ; C3/B2F3
         PEA $6070       ; Pos: 112, 96
         PEA $EC87       ; Anim table ptr
         JMP C3859X      ; Queue OAM fn

; 4F: Sustain Colosseum matchup menu
C3AE0F:  JSR C3B329      ; Handle D-Pad
         LDA $08         ; Pushing A?
         BPL C3B22B      ; Exit if not
         JSR C3B2EC      ; Actor in slot
         BMI C3AE2D      ; Fail if nobody
         STA $0208       ; Set challenger
         JMP C3AA9_      ; Sound + Fade-out
C3AE2D:  JMP C3B66F      ; Buzzer + Mosaic

; Draw Colosseum matchup menu and create members
C3AE34:  LDX #C3B33F     ; C3/B33F
         JSR C3034Y      ; Draw windows
         JSR C36A3C      ; Clear BG3 map A
         LDY #C3B40F     ; Text pointer
         JSR draw20      ; Draw "Select..."
         JSR C3B17D      ; Create party
         LDY #$7C4F      ; Tilemap ptr
         JSR C3BAB_      ; Set position
         JSR C35409      ; Define source
         LDA $0206       ; Opponent
         JSR C334FY      ; Draw name
         LDA $0205       ; Item bet
         LDY #$792B      ; Text position
         JSR C3B263      ; Draw item bet
         JSR C3B255      ; Draw prize
;        LDY #$5000      ; $5000
;        STY $2116       ; Set VRAM ptr
;        JSR C3B10A      ; Blank BG1 tile 1
         JSR C36A15      ; Clear BG1 map A
         LDA $0205       ; Item bet
         CMP #$29        ; Striker?
         BNE C3AE87      ; Branch if not
         LDA $1EBD       ; Shadow at arena?
         BMI C3AE8D      ; Branch if so

; Fork: Build tilemap for Colosseum enemy
C3AE87:  LDA #$7E        ; Bank: 7E
         STA $93         ; Set dst HB
         LDX #$2C01      ; Tile 1, pal 3
         STX $E7         ; Priority: Yes
;        TDC             ; Clear A
         LDA #$08        ; Tiles: 8
         CMP $99         ; 8x8 enemy?
         BEQ C3AF16      ; Branch if so
         ASL A           ; Tiles: 16
C3AF16:  TAX             ; Put in X
         STX $E0         ; Set width
C3AF28:  PHX             ; Set rows left
         LDX $E0         ; Width in tiles
         LDY $00         ; Map index: 0
C3AF2C:  REP #$21        ; 16-bit A; C-
         LDA $E7         ; Tile attribs
         STA [$91],Y     ; Put in tilemap
         INY             ; Map index +1
         INY             ; Map index +1
         INC $E7         ; Tile number +1
         DEX             ; One less column
         BNE C3AF2C      ; Loop till last
         LDA $91         ; Tilemap ptr
         ADC #$0040      ; Go 1 row down
         STA $91         ; Save changes
         PLX             ; Rows left
         DEX             ; One less row
         BNE C3AF28      ; Loop till last
         SEP #$20        ; 8-bit A
         BRA C3AE90      ; Upload tilemaps

; Fork: Create enemy Shadow's name and sprite
C3AE8D:  INC $0201       ; Facing Shadow: Y
         LDX #$166F      ; Actor data adr
         STX $67         ; Memorize it
         LDY #$7C11      ; Text position
         JSR C334CF      ; Draw name
         LDY #C37B42     ; C3/7B42
         JSR qanim2      ; Queue OAM fn
         JSR C382D6      ; OAM mode: 1
         LDA $7E364A,X   ; Sprite flags
         ORA #$02        ; Add H-Flip
         JSR C3EA6E      ; Save changes
         TXY             ; Queue index
         LDX #$6838      ; Pos: 56, 104
         STX $E1         ; Memorize it
         LDA #$03        ; Sprite set: 3
         JSR C3B20E      ; Set ptr, pos
C3AE90:  JMP mvmapx3     ; Upload tilemaps

; Load prize and enemy data for Colosseum matchup
C3AF46:  LDY #$5010      ; $5010
         STY $2116       ; Set VRAM ptr
         TDC             ; Clear A
         LDA $0205       ; Item bet
         REP #$20        ; 16-bit A
         ASL A           ; x2
         ASL A           ; x4
         TAX             ; Index it
         LDA $DFB602,X   ; Prize data
         SEP #$20        ; 8-bit A
         STA $0207       ; Set prize
         XBA             ; Hide Prize flag
         STA $0209       ; Save in RAM
         LDA $DFB600,X   ; Opponent
         STA $0206       ; Save in RAM
         XBA             ; Put it in B
         LDA #$05        ; Enemy data size
         JSR C350F_      ; Enemy x size
         LDA $D27004,X   ; Mold index LSBs
         STA $E9         ; Save in RAM
         REP #$20        ; 16-bit A
         LDA $D27000,X   ; GFX ptr/8 LSBs
         STA $E7         ; Ptr MSBs; flag
         LDA $D27002,X   ; Pal/Mold info
         XBA             ; Palette LSBs
         STA $F1         ; Save in RAM
         SEP #$20        ; 8-bit A

; Fork: Relocate color depth flag
C3AF7D:  STZ $FF         ; Colors: 16
         LDA $E8         ; 8-color palette?
         BPL C3AF85      ; Branch if not
         INC $FF         ; Colors: 8
C3AF85:  LDA #$80        ; 8-color flag
         TRB $E8         ; Clear original

; Fork: Compute enemy graphics address ($E97000+8X)
C3AF8D:  REP #$20        ; 16-bit A
         LDX #$0003      ; Loop counter
         LDA $E7         ; GFX ptr / 8
         STZ $FB         ; Clear HBs
.loop    ASL A           ; Ptr LBs x2
         ROL $FB         ; Ptr HBs x2 + C
         DEX             ; Counter -1
         BNE .loop       ; Loop till 0
         ADC #$7000      ; Add base adr LBs
         STA $F5         ; Set address LBs
         SEP #$20        ; 8-bit A
         LDA $FB         ; GFX pointer HB
         ADC #$E9        ; Add base adr HB
         STA $F7         ; Set address HB

; Fork: Set base address and size for 8x8 mold data
C3AFB9:  LDA $F2         ; 16x16 image?
         BMI C3AFC8      ; Branch if so
         LDY #$A824      ; D2/A824
         LDA #$08        ; 8-tile rows: 8
         BRA C3AFCF      ; Branch below

; Fork: Set base address and size for 16x16 mold data
C3AFC8:  LDY #$AC24      ; D2/AC24
         LDA #$20        ; 8-tile rows: 32
C3AFCF:  STY $E3         ; Set address ptr
         STA $E6         ; Set rows left
         STA $99         ; Set enemy size

; Fork: Load address to enemy's mold
C3AFE0:  TDC             ; Clear A
         LDA $E9         ; Mold index
         JSR x32r        ; Index x32
         LDY $F1         ; 8x8 image?
         BMI C3AFF6      ; Branch if not
         LSR A           ; Index x16
         LSR A           ; Index x8
C3AFF6:  ADC $E3         ; Add base adr
         STA $E0         ; Set src LBs
         SEP #$20        ; 8-bit A
         LDA #$D2        ; Bank: D2
         STA $E2         ; Set src HB
;        LDX $00         ; GFX index: 0

; Fork: Load 8-tile row, blank tiles excluded from mold
C3B001:  PHX             ; Save GFX index
         TDC             ; Clear A
         LDA $E6         ; Rows left
         TAX             ; Index it
         LDA [$E0]       ; Mold row flags
         STA $7E9D88,X   ; For centering
         PLX             ; GFX index
         LDY #$0008      ; Columns: 8
C3B010:  PHY             ; Save column
         ASL A           ; Blank tile here?
         PHA             ; Save mold flags
         BCC C3B01B      ; Branch if blank
         JSR C3B11E      ; Load tile's GFX
         BRA C3B01E      ; Skip a line
C3B01B:  JSR C3B10A      ; Make blank tile
C3B01E:  PLA             ; Mold row flags
         PLY             ; Columns left
         DEY             ; One less left
         BNE C3B010      ; Loop till last

; Fork: Handle next 8-tile row
C3B023:  LDY $E0         ; Mold data adr
         INY             ; Address +1
         STY $E0         ; Save changes
         DEC $E6         ; One less row
         BNE C3B001      ; Loop till last

; Fork: Start positioning enemy based on size
C3B033:  LDX $00         ; 1st: Bottom row
         STX $E0         ; Blank rows: 0
         STZ $E3         ; Clear filled cols
         LDA $99         ; Monster size
         CMP #$08        ; 8x8 tiles?
         BNE C3B079      ; Branch if not

; Fork: Count blank rows, identify filled columns (8x8)
C3B043:  JSR sk_slt6     ; Blank row here?
         BNE C3B050      ; Branch if not
         INC $E0         ; Blank rows +1
         INX             ; Point 1 row up
         BRA C3B043      ; Test next row
C3B050:  LDX #$0007      ; 1st: Topmost row
C3B052:  JSR sk_slt6     ; Row's filled cols
         TSB $E3         ; Add to prior rows'
         DEX             ; Point 1 row down
         BPL C3B052      ; Loop till 8 done

; Fork: Position 8x8 enemy based on empty columns
C3B060:  LDY #$3A51      ; Base position
         BRA C3B09X      ; Branch below

; Fork: Count blank rows, identify R-Half's filled columns (16x16)
C3B079:  JSR sk_slt6     ; Row's right half
         ORA $7E9D8A,X   ; L-Half empty too?
         BNE C3B08C      ; Branch if not
         INC $E0         ; Blank rows +1
         INX             ; 8-tile index +1
         INX             ; 8-tile index +1
         CPX #$0020      ; Done all 16 rows?
         BNE C3B079      ; Test next row
C3B08C:  LDX #$001E      ; 1st: Topmost row
C3B08E:  JSR sk_slt6     ; R-Half filled cols
         TSB $E3         ; Add to prior rows'
         DEX             ; 8-tile index -1
         DEX             ; 8-tile index -1
         BPL C3B08E      ; Loop till last

; Fork: Position 16x16 enemy based on empty columns
C3B09D:  LDY #$3849      ; Base position
C3B09X:  STY $E7         ; Save tilemap ptr
         TDC             ; Relative pos: 0
         SEC             ; Max loops: 8
C3B09Y:  ROR $E3         ; Empty column?
         BCS C3B0A5      ; Branch if not
         INC A           ; Relative pos +1
         BRA C3B09Y      ; Test next column
C3B0A5:  LSR A           ; If odd-numbered:
         ASL A           ; Relative pos -1
         REP #$21        ; 16-bit A; C-
         ADC $E7         ; Add base address
         STA $E7         ; Save tilemap ptr
         SEP #$20        ; 8-bit A

; Fork: Adjust Y position based on enemy and blank rows
C3B0B7:  LDY $E0         ; Blank rows
         TDC             ; Clear A
         LDA $0206       ; Enemy number
         TAX             ; Index it
         LDA $ECE800,X   ; Modifier index
         BEQ C3B0D3      ; If index = 0
         DEC A           ; Index = 1?
         BEQ C3B0F8      ; Branch if so
         DEC A           ; Index = 2?
         BEQ C3B0F2      ; Branch if so
         DEC A           ; Index = 3?
         BEQ C3B0EA      ; Branch if so

; Fork: Shift enemy down by $E0 - 3
C3B0EE:  DEY             ; Blank rows -1
         DEY             ; Blank rows -1

; Fork: Shift enemy down by $E0 - 1
C3B0F2:  DEY             ; Blank rows -1
         BPL C3B0F8      ; Branch if 0+
         LDY $00         ; Blank rows: 0

; Fork: Shift enemy down by $E0
C3B0F8:  TYA             ; Blank rows
         JSR x64         ; x64
         ADC $E7         ; Add map ptr
         BRA C3B0DD      ; Save map ptr

; Fork: Shift enemy down by $E0 + 1
C3B0EA:  INY             ; Blank rows +1
         BRA C3B0F8      ; Shift enemy

; Fork: Shift enemy up by 3 tiles
C3B0D3:  REP #$21        ; 16-bit A; C-
         LDA $E7         ; Tilemap ptr
         SBC #$00BF      ; Go 3 rows up
C3B0DD:  STA $91         ; Save map ptr

; Fork: Load colors
C3B02D:  LDA $F1         ; Palette index
         JSR x64         ; Clear flags
         LSR A           ; x64 -> x32
         LSR A           ; x16 colors
         ADC #$7820      ; Add base adr
         TAX             ; Set src LBs
         SEP #$20        ; 8-bit A
         LDA #$D2        ; Src HB: D2
         LDY #$30A9      ; Dst: 7E/30A9
         JMP C30F8E      ; Load colors

; Create a blank tile at current location in VRAM
C3B10A:  LDY #$0010      ; Rows: 8
         JMP C3DD7A      ; Make blank tile

; Upload current tile from monster's tileset
C3B11E:  TXY             ; GFX index
         LDA $FF         ; Has 8 colors?
         BNE C3B129      ; Branch if so
         LDX #$0010      ; Bytes: 32

; Fork: Upload specified number of words
C3B145:  REP #$20        ; 16-bit A
C3B14A:  LDA [$F5],Y     ; 8 px, 2 planes
         STA $2118       ; Save in VRAM
         INY             ; GFX index +1
         INY             ; GFX index +1
         DEX             ; Counter -1
         BNE C3B14A      ; Loop till 0
         SEP #$20        ; 8-bit A
         TYX             ; GFX index
         RTS

; Fork: Load 8-color tile, encode as 4bpp
C3B129:  LDX #$0008      ; Bytes: 16
         PHX             ; Push this value
         JSR C3B145      ; Load rows' LSBs
         PLX             ; Rows left: 8
         TDC             ; GFX rows' MSBs
C3B136:  PHX             ; Save counter
         LDA [$F5],Y     ; GFX row's MSBs
         TAX             ; Put in X
         STX $2118       ; Save in VRAM
         INY             ; GFX index +1
         PLX             ; Rows left
         DEX             ; One less row
         BNE C3B136      ; Loop till last
         TYX             ; GFX index
         RTS

; Create party members for Colosseum
C3B17D:  LDX #$0006      ; 1st: Member 4
.loop    PHX             ; Save index
         JSR C34354      ; Actor's address
         BEQ .next       ; Skip if nobody
         REP #$20        ; 16-bit A
         LDA.L colobj,X  ; Sprite position
         STA $E1         ; Memorize it
         LDA.L colnm,X   ; Name position
         TAY             ; Put it in Y
         SEP #$20        ; 8-bit A
         TDC             ; Clear A
         TXA             ; Member slot x2
         LSR A           ; Member slot
         TAX             ; Reindex it
         LDA $69,X       ; Actor's slot
         PHA             ; Memorize it
         JSR C334CF      ; Draw actor name
         LDY #C37B42     ; C3/7B42
         JSR qanim2      ; Queue OAM fn
         TXY             ; Queue index
;        TDC             ; Clear A
         PLA             ; Actor's slot
         JSR C3B1F3      ; Init sprite
.next    PLX             ; Member index
         DEX             ; Member index -1
         DEX             ; Member index -1
         BPL .loop       ; Loop till < 0
         RTS

; Draw Colosseum prize
C3B255:  LDA $0209       ; Hide prize?
         BNE C3B286      ; Use "???" if so
         LDA $0207       ; Prize
         LDY #$790D      ; Tilemap ptr
C3B263:  PHA             ; Save item
         JSR C3BAB_      ; Set position
         PLA             ; Item
C3B26_:  JSR C3C068      ; Load item name
         JMP C38847      ; Draw item name

; Fork: Draw question marks
C3B286:  LDY #C3B427     ; Text pointer
         JMP draw20      ; Draw string

; Update Colosseum challenger's name and sprite
C3B2A5:  PHB             ; Save DB
         TDC             ; Zero A
         PHA             ; Put on stack
         PLB             ; Set DB to 0
         PEA $7C75       ; Name position
         JSR C32D0B      ; Actor's address
         BEQ C3B2DF      ; Blank if nobody
         STY $67         ; Memorize it
         PLY             ; Text position
         JSR C334CF      ; Draw actor name
         LDA #$02        ; Temp sprite: Y
         TSB $47         ; Set menu flag
         LDY #C37B42     ; C3/7B42
         JSR qanim2      ; Queue OAM fn
         LDA #$02        ; Anim mode: 2
         JSR C382D8      ; Set sprite's
         TXY             ; Queue index
         LDX #$68B8      ; Pos: 184, 104
         STX $E1         ; Memorize it
         JSR C3B2EC      ; Actor's slot
         JSR C3B1F3      ; Init sprite
         BRA C3B2E2      ; Skip 3 lines
C3B2DF:  PLY             ; Name position
         JSR C3BAB_      ; Set position
         JSR C3630_      ; Blank name
C3B2E2:  PLB             ; Restore DB
         SEC             ; Set to requeue
         RTS

; Get party actor indicated by cursor
C3B2EC:  JSR C3BFC2      ; X = Cursor slot
         LDA $69,X       ; Actor slot
         RTS

; Update "VS" symbol from Colosseum
C3B2F3:  BNE C3B31E      ; If mode 1

; Mode 0: Initialize sprite
C3B2FB:  JSR C3120_      ; Set pose timer

; Mode 1: Sustain sprite
C3B31E:  JMP C30860      ; Set OAM; requeue

; Load arena graphics, tilemap, and colors
C3B34B:  LDX #$0008      ; Src index
.loop    LDA $E7168C,X   ; GFX ptr byte
         PHA             ; Push on stack
         DEX             ; Src index -1
         BPL .loop       ; Loop till < 0
         PLX             ; Src: E8/0C49
         PLA             ; Src bank
         LDY #$6800      ; VRAM: $6800
         JSR C3B3C6      ; Upload GFX A
         SEP #$20        ; 8-bit A
         PLX             ; Purge stack
         PLA             ; Purge stack

; Fork: Unpack and upload graphics for upper half
C3B36E:  PLX             ; Src: E8/1841
         PLA             ; Src bank
         LDY #$7000      ; VRAM: $7000
         JSR C3B3C6      ; Upload GFX B

; Fork: Unpack battle tilemap, convert to menu format
C3B391:  LDA $E7187A     ; E7/652B
         TAX             ; Set src LBs
         SEP #$20        ; 8-bit A
         LDA #$E7        ; Bank: E7
         JSR C3B3F2      ; Unpack map
         LDX #$057E      ; Tile index
C3B3DC:  LDA $7EB68D,X   ; Attributes
         SEC             ; Prepare SBC
         SBC #$0380      ; Tile num -896
         STA $7E5949,X   ; Put in BG2a
         DEX             ; Index -1
         DEX             ; Index -1
         BPL C3B3DC      ; Loop till < 0

; Fork: Load colors
C3B3A5:  SEP #$20        ; 8-bit A
         LDX #$0AB0      ; Src: E7/0AB0
         LDA #$E7        ; Src HB: E7
         LDY #$30C9      ; Dst: 7E/30C9
         PEA $005F       ; Colors: 48
         JMP C30F91      ; To pal 4-6

; Decompress data for arena background
C3B3F2:  LDY #$B68D      ; Dst: 7E/B68D
         JSR C3DD6_      ; Unpack data
         REP #$20        ; 16-bit A
         RTS

; Upload current graphics set for arena background
C3B3C6:  STY $2116       ; Set VRAM ptr
         JSR C3B3F2      ; Unpack GFX
         LDX $00         ; Index: 0
C3B3C8:  LDA $7EB68D,X   ; 8 px, 2 planes
         STA $2118       ; Save in VRAM
         INX             ; Index +1
         INX             ; Index +1
         CPX #$1000      ; Done 4096 bytes?
         BNE C3B3C8      ; Loop if not
         RTS

; 24: Initialize shop menu
C3B466:  JSR C3352X      ; Reset/Stop stuff
         JSR C36A08      ; Set Win1 bounds
         STZ $4A         ; Sell list scroll: 0
         JSR C3B886      ; Relocate cursor
         JSR C3B8C4      ; Draw menu; desc
         JSR C37E2B      ; Set to shift text
         JSR C3C018      ; Adjust BG3 shifting
         LDA #$25        ; Queue: Sustain menu
         JMP C346A_      ; Queue cursor OAM

; 25: Sustain main shop menu
C3B49B:  JSR C3B88C      ; Handle D-Pad
         LDA $08         ; Pushing A?
         BPL C3B4AA      ; Branch if not
         JSR C30EB2      ; Sound: Click
         JSR C32D0B      ; X = Cur slot x2
         JMP (C3B79A,X)  ; Handle option

; Fork: Handle B
C3B4AA:  LDA $09         ; Pushing B?
         BPL C3B4BC      ; Exit if not
         JSR C30EA9      ; Sound: Cursor
C3B4B6:  LDA #$FF        ; Null value
C3B4B8:  STA $27         ; Queue menu exit
         STZ $26         ; Next: Fade-out
C3B4BC:  RTS

; 26: Handle buy item list
C3B4BD:  JSR C38590      ; Reset/Stop desc
         JSR C30F39      ; Queue text upload
         JSR C3B8A6      ; Handle D-Pad
         JSR C3B9F5      ; Draw owned values

; Fork: Handle B
C3B4CD:  LDA $09         ; Pushing B?
         BPL C3B4D9      ; Branch if not
         JMP C3B62E      ; Sound + Exit

; Fork: Handle A
C3B4D9:  LDA $08         ; Pushing A?
         BPL C3B4BC      ; Exit if not

; Fork: Define buy quantity limit
C3B82F:  LDA $64         ; Qty in stock
         CLC             ; Prepare ADC
         ADC $65         ; Add qty worn
         STA $69         ; Set total owned
         LDA #$64        ; Quantity: 99 (+1)
;        SEC             ; Prepare SBC
         SBC $69         ; Sub total owned
         STA $6A         ; Set buy limit

; Fork: Invoke buy order menu if justified
C3B7E6:  JSR C3BFC2      ; Selected item
         JSR itmtype     ; Item's class
         DEC A           ; Tool?
         BPL C3B80A      ; Branch if not
         LDY #C3C3F5     ; Text pointer
         LDA $64         ; Owning any?
         BNE C3B87X      ; Fail if so

; Fork: Check if enough money
C3B80A:  LDA $1862       ; GP > 65535?
         BNE C3B83E      ; Ample if so
         JSR C32D0B      ; X = Cur slot x2
         LDY #C3C3CE     ; Text pointer
         REP #$20        ; 16-bit A
         LDA $7E9F09,X   ; Item's price
         DEC A           ; Subtract 1
         CMP $1860       ; Got enough?
         SEP #$20        ; 8-bit A
         BCS C3B87X      ; Fail if not

; Fork: Check if owning 99
C3B83E:  LDA $69         ; Qty owned/worn
         CMP #$63        ; Under 99?
         BCC C3B850      ; Okay if so
         LDY #C3C3E2     ; Text pointer
C3B87X:  JSR C30EC0      ; Play buzzer
C3B87Y:  JSR C302F9      ; Draw "Too many!"

; Fork: Set to return to Buy menu after delay
C3B87D:  LDA #$28        ; C3/B60E
C3B87F:  STA $26         ; Next: Exit
         LDA #$20        ; Frames: 32
         STA $20         ; Set delay
         RTS

; Fork: Can buy item
C3B850:  JSR C30EB2      ; Sound: Click
         LDX #$3408      ; Pos: 8, 52
         STX $55         ; Set cursor's
         INC $26         ; Next: Order menu
         JSR C3BAA5      ; Draw blue text
         LDA $26         ; Current menu
         CMP #$2A        ; Sell menu?
         BEQ .skip       ; Branch if so
         JSR C3BFC2      ; Selected item
         PHA             ; Memorize it
         JSR C3BAF8      ; Draw power info
         JSR C3B9F5      ; Draw owned values
         PLA             ; Selected item
         JSR C3B26_      ; Draw item name
.skip    LDA #$01        ; Quantity: 1
         STA $28         ; Set order size
         JSR C3BBAE      ; Draw size, value
         LDY #C3C396     ; Text pointer
         JSR C302F9      ; Draw "How many?"

; Fork: Switch screen
C3B86D:  STZ $3A         ; BG2 X: 0
         INC $3C         ; BG2 Y: 256
         JSR C3A796      ; Clear desc GFX
         LDY #$7CCD      ; Dest: 7E/7CCD
         JMP C3A740      ; Build desc map

; 27: Handle buy order menu
C3B505:  JSR C3B4E6      ; Set desc flags
         JSR C38308      ; Set desc ptrs
         JSR C35730      ; Load description
         JSR C3BBAE      ; Draw size, value

; Fork: Buy quantity +1
C3B511:  LDA $0B         ; Semi-auto keys
         LSR A           ; Pushing right?
         BCC C3B539      ; Branch if not
         JSR C30EA3      ; Sound: Cursor
         LDA $28         ; Buy quantity
         CMP $6A         ; At capacity?
         BEQ C3B59E      ; Abort if so
         INC $28         ; Quantity +1
         JSR C3BB53      ; Get new cost
         LDX $1860       ; Gold LBs
         CPX $F1         ; C- if < cost's
         LDA $1862       ; Gold HB
         SBC $F3         ; So, GP < cost?
         BCS C3B538      ; Exit if not
         DEC $28         ; Quantity -1
C3B538:  RTS

; Fork: Buy quantity -1
C3B539:  LSR A           ; Pushing left?
         BCC C3B54A      ; Branch if not
         JSR C30EA3      ; Sound: Cursor
         DEC $28         ; Quantity -1
         BEQ C3B59A      ; Revert if 0

; Fork: Buy quantity +10
C3B54A:  JSR key_up      ; Pushing up?
         BEQ C3B583      ; Branch if not
         JSR C30EA3      ; Sound: Cursor
         LDA #$09        ; Quantity: 9
         CLC             ; Prepare ADC
         ADC $28         ; Add order size
         CMP $6A         ; Above capacity?
         BCS C3B599      ; Exit if so
         INC A           ; Quantity +1
         STA $28         ; Set order size
         JSR C3BB53      ; Get new cost
         LDX $1860       ; Gold LBs
         CPX $F1         ; C- if < cost's
         LDA $1862       ; Gold HB
         SBC $F3         ; So, GP < cost?
         BCS C3B599      ; Exit if not
         LDA $28         ; Buy quantity
         SBC #$09        ; Subtract 10
         BRA C3B597      ; Save changes

; Fork: Buy quantity -10
C3B583:  BIT #$04        ; Pushing down?
         BEQ C3B59E      ; Branch if not
         JSR C30EA3      ; Sound: Cursor
         LDA $28         ; Buy quantity
         SEC             ; Prepare SBC
         SBC #$0A        ; Subtract 10
         BMI C3B59A      ; Use 1 if < 0
         BEQ C3B59E      ; Abort if 0
C3B597:  STA $28         ; Save changes
C3B599:  RTS

; Fork: Set buy quantity to 1
C3B59A:  LDA #$01        ; Quantity: 1
         STA $28         ; Set order size

; Fork: Handle B
C3B59E:  LDA $09         ; Pushing B?
         BPL C3B5AA      ; Branch if not
         JSR C30EA9      ; Sound: Cursor
         BRA C3B612      ; Exit submenu

; Fork: Handle A
C3B5AA:  LDA $08         ; Pushing A?
         BPL C3B599      ; Exit if not
         JSR C30ECE      ; Sound: Cash
         JSR C3BFC2      ; Bought item
         LDY #$00FF      ; Item slot: 256
C3B5BC:  CMP $1869,Y     ; Slot holds it?
         BEQ C3B5E1      ; Qty +1 if so
         DEY             ; Item slot -1
         BPL C3B5BC      ; Loop till < 0
C3B5C9:  INY             ; Item slot +1
         JSR C3BFCF      ; Empty slot?
         BNE C3B5C9      ; Try next if not

; Fork: Put bought item in empty slot
C3B5D3:  JSR sk_slt6     ; Bought item
         STA $1869,Y     ; Assign to slot
         LDA $28         ; Quantity bought
         BRA C3B5E7      ; Branch below

; Fork: Add quantity bought to stock
C3B5E1:  LDA $28         ; Quantity bought
         CLC             ; Prepare ADC
         ADC $1969,Y     ; Add qty owned
C3B5E7:  STA $1969,Y     ; Set qty owned

; Fork: Reduce party's GP, leave submenu
C3B5EA:  JSR C3BB53      ; Get order value
         LDY #$0003      ; Loop counter
         LDX $00         ; Index: 0
         SEC             ; Prepare SBC
.loop    LDA $1860,X     ; GP byte
         SBC $F1,X       ; Deduct cost's
         STA $1860,X     ; Save changes
         INX             ; Index +1
         DEY             ; Counter -1
         BNE .loop       ; Loop till 0
         JSR C332CE      ; Cap GP
         LDY #C3C3BD     ; Text pointer
         JMP C3B87Y      ; Draw "Bye!"

; Set description to be displayed and to ignore joypad
C3B4E6:  LDA #$20        ; D-Pad: Ignore
         TSB $45         ; Set desc flag
C3B4E_:  LDA #$10        ; Description: On
         TRB $45         ; Set menu flag
C3B4EE:  RTS

; 28: Return to Buy menu after delay
C3B60E:  LDA $20         ; Delay expired?
         BNE C3B4EE      ; Exit if not
C3B612:  JMP C3B7B3      ; Leave submenu

; 29: Handle sell item list
C3B616:  JSR C38590      ; Reset/Stop desc
         JSR itm_shr     ; Handle D-Pad, etc.
         JSR C38590      ; Description: Off
         BCS C3B4EE      ; Exit if L or R

; Fork: Handle B
C3B628:  LDA $09         ; Pushing B?
         BPL C3B634      ; Branch if not
C3B62E:  JSR C3858C      ; Scrollbar: Off
         STZ $47         ; Actors: Off
         JSR C3B886      ; Relocate cursor
         JSR C3B94E      ; Clear BG1 maps
         JSR C31368      ; Upload them
         JSR C30F39      ; Queue BG3 upload
         JSR C3B95A      ; Clean up BG3
         LDA #$25        ; Next: Shop menu
         JSR C398F_      ; Sound: Cursor
         JSR C36904      ; Reset BGs' X/Y
C3B62X:  LDY #C3C36F     ; Text pointer
         JSR C302F9      ; Draw greeting
C3B62Y:  LDY #C3C326     ; Text pointer
         JMP C302F9      ; Draw options

; Fork: Handle A
C3B634:  LDA $08         ; Pushing A?
         BPL C3B4EE      ; Exit if not
         JSR C3BFCB      ; Selected item
         BEQ C3B66F      ; Fail if nothing
         PHA             ; Memorize item
         PHA             ; Do it again
         JSR C3858C      ; Scrollbar: Off
         JSR C36A15      ; Blank item list
         JSR C31368      ; Upload it now
         JSR C3B850      ; Init order menu
         JSR C3BFCB      ; Y = List slot
         LDA $1969,Y     ; Quantity owned
         JSR C3BC87      ; Draw qty owned
         JSR C3BF66      ; Draw qty worn
         PLA             ; Selected item
         JSR C3BAF8      ; Draw power info
         PLA             ; Selected item
         JSR C3B26_      ; Draw item name

; Fork: Draw sell order size and value
C3BBB7:  JSR C3BB65      ; Get order value
C3BBD7:  JSR C30582      ; Turn into text
         LDX #$7AA1      ; Text position
         JSR C304AC      ; Draw 7 digits
         LDY #C3C33D     ; Text pointer
         JSR draw2c      ; Draw "GP"
         JSR C38085      ; Color: User's
         LDA $28         ; Order size
         LDX #$7A2B      ; Text position
         JMP C304B_      ; Draw 2 digits

; Fork: Invalid selection
C3B66F:  JSR C30EC0      ; Play buzzer
         LDY #C33064     ; C3/3064
         JMP qanim0      ; Queue mosaic

; Draw buy order size and value
C3BBAE:  JSR C3BB53      ; Get order value
         BRA C3BBD7      ; Draw value, size

; 2A: Handle sell order menu
C3B676:  JSR C3B4E6      ; Set desc flags
         JSR C3B4F8      ; Load description
         JSR C3BBB7      ; Draw qty, offer

; Fork: Sell quantity +1
C3B682:  LDA $0B         ; Semi-auto keys
         LSR A           ; Pushing right?
         BCC C3B695      ; Branch if not
         JSR C30EA3      ; Sound: Cursor
         LDA $28         ; Sell quantity
         CMP $64         ; Can sell more?
         BEQ C3B6E0      ; Abort if not
         INC A           ; Quantity +1
         BRA C3B6DE      ; Handle B

; Fork: Sell quantity -1
C3B695:  LSR A           ; Pushing left?
         BCC C3B6A6      ; Branch if not
         JSR C30EA3      ; Sound: Cursor
         DEC $28         ; Quantity -1
         BEQ C3B6DC      ; Revert if 0

; Fork: Sell quantity +10
C3B6A6:  JSR key_up      ; Pushing up?
         BEQ C3B6C4      ; Branch if not
         JSR C30EA3      ; Sound: Cursor
         LDA #$0A        ; Quantity: 10
         CLC             ; Prepare ADC
         ADC $28         ; Add order size
         CMP $64         ; Owning enough?
         BCC C3B6DE      ; Retain if so

; Fork: Limit sell quantity
C3B6BE:  LDA $64         ; Quantity owned
         BRA C3B6DE      ; Set order size

; Fork: Sell quantity -10
C3B6C4:  BIT #$04        ; Pushing down?
         BEQ C3B6E0      ; Branch if not
         JSR C30EA3      ; Sound: Cursor
         LDA $28         ; Sell quantity
         SEC             ; Prepare SBC
         SBC #$0A        ; Subtract 10
         BEQ C3B6E0      ; Abort if 0
         BPL C3B6DE      ; Retain if > 0

; Fork: Set sell quantity to 1
C3B6DC:  LDA #$01        ; Quantity: 1
C3B6DE:  STA $28         ; Set order size

; Fork: Handle B
C3B6E0:  LDA $09         ; Pushing B?
         BPL C3B6FB      ; Branch if not
         JSR C30EA9      ; Sound: Cursor
         BRA C3B756      ; Exit submenu

; Fork: Handle A
C3B6FB:  LDA $08         ; Pushing A?
         BPL C3B7E5      ; Exit if not
         JSR C30ECE      ; Sound: Cash
         JSR C3BB65      ; Get order value
         REP #$21        ; 16-bit A; C-
         TXA             ; Cost LBs
         ADC $1860       ; Add gold LBs
         STA $1860       ; Set gold LBs
         SEP #$20        ; 8-bit A
         LDA $1862       ; Gold HB
         ADC $F3         ; Add cost's + C
         STA $1862       ; Save changes
         JSR C3BFCB      ; Define Y
         LDA $64         ; Quantity owned
         CMP $28         ; Sold all of it?
         BEQ C3B734      ; Branch if so
         SBC $28         ; Deduct qty sold
         BRA C3B73D      ; Assign to slot

; Fork: Clear item slot, leave submenu
C3B734:  LDA #$FF        ; Empty item
         STA $1869,Y     ; Put in slot
         TDC             ; Quantity: 0
C3B73D:  STA $1969,Y     ; Assign to slot
         JSR C332CE      ; Cap GP
         LDY #C3C3BD     ; Text pointer
         JSR C302F9      ; Draw "Bye!"
         LDA #$2B        ; Next: Exit
         JMP C3B87F      ; Delay: 32

; 2B: Return to Sell menu after delay
C3B752:  LDA $20         ; Delay expired?
         BNE C3B7E5      ; Exit if not
C3B756:  JSR C36904      ; Reset BGs' X/Y
C3B7CF:  JSR C3BC02      ; Draw list; bar
C3B7D2:  JSR C3B95A      ; Draw title, GP
         JSR C3B62Y      ; Draw options
         LDY #C3C3A2     ; Text pointer
         JSR C302F9      ; Draw question
         LDA #$29        ; C3/B616
C3B7E3:  STA $26         ; Next: Sell list
C3B7E5:  RTS

; Initialize Sell menu
C3B7C7:  JSR C3BBE0      ; Cursor/list/bar
         BRA C3B7D2      ; Branch above

; Jump table for shop menu options
C3B79A:  dw C3B7A3       ; BUY
         dw C3B7C7       ; SELL
         dw C3B4B6       ; EXIT

; Initialize Buy menu
C3B7A3:  LDY #C3B8AF     ; C3/B8AF
         JSR C305FE      ; Load navig data
         JSR C3B8A9      ; Relocate cursor
         JSR C3C09A      ; Make chars, signs
C3B7B3:  LDA #$01        ; X: 256
         STA $3A         ; Set BG2 X-Pos
         STZ $3C         ; BG2 Y: 0
         JSR C3B986      ; Draw all text
         JSR C3BCFD      ; Build sign list
         LDA #$26        ; C3/B4BD
         BRA C3B7E3      ; Next: Buy list

; Draw main shop menu and submenu windows, set to update description
C3B8C4:  JSR bg2c_cl     ; Clear BG2 maps
         LDX #C3BC1B     ; C3/BC1B
         JSR C3034Y      ; Draw windows
         JSR C3B94E      ; Clear BG1 maps
         JSR C3B95A      ; Draw title, GP
         JSR C3B62X      ; Draw options, etc.
         JSR C31B99      ; Queue desc anim
         JSR C30E60      ; Upload BG2 C+D
mvmapx5: JSR C30E7C      ; Upload BG3 C+D
mvmapx4: JSR C30E44      ; Upload BG1 C+D
mvmapx3: JSR C30E52      ; Upload BG2 A+B
mvmapx2: JSR C30E28      ; Upload BG1 A+B
         JMP C30E6E      ; Upload BG3 A+B

; Clear BG1 tilemaps
C3B94E:  JSR C36A23      ; Clear BG1 map D
bg1c_cl: JSR C36A1E      ; Clear BG1 map C
bg1b_cl: JSR C36A19      ; Clear BG1 map B
         JMP C36A15      ; Clear BG1 map A

; Draw shop title and party's gold, clear rest of BG3
C3B95A:  JSR bg3c_cl     ; Clear BG3 maps
         LDA $0201       ; Shop number
         XBA             ; Put in B
         LDA #$09        ; Shop data size
         JSR C350F_      ; Number x size
         STX $67         ; Set shop index
;        TDC             ; Clear A
         LDA $C47AC0,X   ; Shop flags
         AND #$07        ; Title number
         ASL A           ; Double it
         TAX             ; Index it
         REP #$20        ; 16-bit A
         LDA.L C3C00C-2,X; Text pointer
         TAY             ; Put in Y
         JSR draw2c      ; Draw title
         LDY #C3C338     ; Text pointer
         JSR draw2c      ; Draw "GP"
         JSR C3058X      ; Convert GP to text
         LDX #$7A33      ; Text position
         JMP C304AC      ; Draw GP total

; Draw all text for Buy menu
C3B986:  JSR C3B95A      ; Draw title, GP
         LDY #C3C385     ; Text pointer
         JSR C302F9      ; Draw "Help..."
         JSR C3C2E1      ; Draw Owned, Eqpd
         LDY #$0007      ; Menu slot: 8
         STY $F5         ; Set as current
         STZ $54         ; List size: 0
C3B998:  TDC             ; Clear A
         LDA $F5         ; Menu slot
         JSR x64         ; x64
         ASL A           ; x128
         ADC #$7A0D      ; Add base pos
         STA $7E9E89     ; Set position
         ADC #$001A      ; Now price pos
         TAY             ; Save it in Y
         LDA $F5         ; Menu slot
         ADC $67         ; Add shop index
         TAX             ; Index it
         SEP #$20        ; 8-bit A
         LDA $C47AC1,X   ; Shop item
         LDX $F5         ; Menu slot
         JSR st_9D89     ; Assign item
         CMP #$FF        ; Nothing?
         BEQ C3B9E4      ; Branch if so
         PHY             ; Save price pos
         PHA             ; Memorize item
         JSR C3B26_      ; Draw item name
         PLA             ; Current item
         JSR itm_prc     ; Base price
         TAY             ; Save it in Y
         LDX $67         ; Shop index
         LDA $C47AC0,X   ; Shop flags
         AND #$0038      ; Get modifier
         LSR A           ; Put in b2-b4
         LSR A           ; Put in b1-b3
         TAX             ; Index it
         TYA             ; Base price
         JSR (C3BA41,X)  ; Apply modifiers
         STA $F3         ; Memorize result
         LDA $F5         ; Menu slot
         ASL A           ; Double it
         TAX             ; Index it
         LDA $F3         ; Modified price
         STA $7E9F09,X   ; Set item price
         JSR C3052E      ; Turn into text
         PLX             ; Price position
         JSR C3049A      ; Draw price
         INC $54         ; List size +1
C3B9E4:  DEC $F5         ; Menu slot -1
         BPL C3B998      ; Loop till < 0
         JSR C3BC57      ; Get qties owned
C3B9F5:  JSR C3BC84      ; Draw qty owned
         JSR C3BFC2      ; Item in slot
         JMP C3BF69      ; Draw qty worn

; Jump table for price multipliers
C3BA41:  dw C3BA59       ; 100%
         dw C3BA4F       ; 150%
         dw C3BA56       ; 200%
         dw C3BA58       ; 50%
         dw C3BA5A       ; 50% if female leads, 150% otherwise
         dw C3BA76       ; 50% if male leads, 150% otherwise
         dw C3BA92       ; 50% if Edgar leads

; Double price (unused)
C3BA56: ; ASL A          ; Double price
        ; RTS

; Halve price if female leads, raise by 50% otherwise (unused)
C3BA5A: ; SEP #$20       ; 8-bit A
        ; LDA $0202      ; Leader sprite
        ; BEQ C3BA71     ; -50% if Terra
        ; CMP #$06       ; Celes?
        ; BEQ C3BA71     ; Halve if so
        ; CMP #$08       ; Relm?
        ; BEQ C3BA8D     ; +50% if not
C3BA71: ; REP #$20       ; 16-bit A
        ; BRA C3BAA0     ; Halve price

; Halve price if male leads, raise by 50% otherwise (unused)
C3BA76: ; SEP #$20       ; 8-bit A
        ; LDA $0202      ; Leader sprite
        ; BEQ C3BA8D     ; +50% if Terra
        ; CMP #$06       ; Celes?
        ; BEQ C3BA8D     ; +50% if so
        ; CMP #$08       ; Relm?
        ; BEQ C3BA8D     ; +50% if so
        ; REP #$20       ; 16-bit A
        ; BRA C3BAA0     ; Halve price
C3BA8D: ; REP #$20       ; 16-bit A
        ; TYA            ; Base price
C3BA4F: ; STA $E7        ; Save price
        ; LSR A          ; Halve it
        ; CLC            ; Prepare ADC
        ; ADC $E7        ; Add base price
        ; RTS

; Halve price if Edgar leads
C3BA92:  SEP #$20        ; 8-bit A
         LDA $0202       ; Leader sprite
         CMP #$04        ; Edgar?
         REP #$20        ; 16-bit A
         BEQ C3BAA0      ; Halve if so
         TYA             ; Base price
         RTS             ; Exit
C3BAA0:  TYA             ; Base price
C3BA58:  LSR A           ; Halve it
C3BA59:  RTS

; Draw blue text for shop order menu, position item name
C3BAA5:  JSR C30F39      ; Queue BG3 upload
         JSR C3B95A      ; Clean up BG3
         JSR C3C2E1      ; Draw Owned, Eqpd
         LDY #$7A0F      ; Tilemap ptr
C3BAB_:  REP #$20        ; 16-bit A
         TYA             ; Tilemap ptr
C3BAB3:  STA $7E9E89     ; Set item position
         SEP #$20        ; 8-bit A
         TDC             ; Clear A
         RTS

; Draw power-related text for Buy or Sell menu
C3BAF8:  JSR itmtype     ; Item's class
         CMP #$06        ; Consumable?
         BEQ C3BB52      ; Exit if so
         DEC A           ; Tool?
         BMI C3BB52      ; Exit if so
         PHA             ; Save class
         LDY #$7BA9      ; Text position
         JSR C387B3      ; Draw power
         LDY #C3C361     ; Ptr: "Defense"
         PLA             ; Weapon class?
         BNE C3BB3_      ; Branch if not

; Fork: Handle weapon
C3BB30:  LDY #C3C357     ; Text pointer
C3BB3_:  JSR draw2c      ; Draw "Bat Pwr"
         LDY #C3C36B     ; Text pointer
C3BB4C:  JSR C302F9      ; Draw "…"
C3BB52:  JMP C38085      ; Color: User's

; Compute buy order value
C3BB53:  JSR C32D0B      ; Define X
         REP #$20        ; 16-bit A
         LDA $7E9F09,X   ; Price asked
         BRA C3BB81      ; Price x size

; Compute sell order value
C3BB65:  JSR C3BFCB      ; Selected item
         JSR itm_prc     ; Base price
         LSR A           ; Halve it

; Compute buy or sell order value
C3BB81:  TAY             ; Item value
         SEP #$20        ; 8-bit A
         LDA $28         ; Item quantity
         JSR mul16       ; Value x size
         STX $F1         ; Set total LBs
         STA $F3         ; Set total HB
         RTS             ; ^ Keep X as-is

; Initialize cursor data and draw inventory for Sell menu
C3BBE0:  JSR bg1_2x1     ; BG1: 64x32 at $00
         JSR C31AEF      ; Load navig data
         LDA $4A         ; Scroll position
         STA $50         ; Set list V-Slot
         JSR C37D25      ; Relocate cursor
C3BC02:  JSR C37E0D      ; Draw inventory
C3BC05:  LDY #$0070      ; V-Speed: 0.44 px
         LDA #$58        ; Y: 88
C3BC0X:  PHA             ; Save Y position
         PHY             ; Save V-Speed
         JSR C3091F      ; Create scrollbar
         REP #$20        ; 16-bit A
         PLA             ; Vertical speed
         STA $7E354A,X   ; Set scrollbar's
         SEP #$20        ; 8-bit A
         PLA             ; Y position
C3BC0Y:  STA $7E34CA,X   ; Set scrollbar's
         RTS

; Compile quantity owned of each item in Buy menu
C3BC57:  LDX #$0007      ; Menu slot: 8
C3BC5F:  LDY #$00FF      ; Stock slot: 256
         JSR sk_slt6     ; Shop item
C3BC66:  CMP $1869,Y     ; In stock slot?
         BEQ C3BC76      ; Branch if so
         DEY             ; Stock slot -1
         BPL C3BC66      ; Loop till < 0
         TDC             ; Quantity: 0
         BRA C3BC79      ; Skip a line
C3BC76:  LDA $1969,Y     ; Quantity owned
C3BC79:  STA $7E9DC9,X   ; Set shop item's
         DEX             ; Menu slot -1
         BPL C3BC5F      ; Loop till < 0
         RTS

; Load and draw quantity owned of item in Buy menu
C3BC84:  JSR C3BFC2      ; X = List slot
         LDA $7E9DC9,X   ; Quantity owned
C3BC87:  STA $64         ; Set as current
         LDX #$7B3F      ; Text position
         JMP C304B_      ; Draw 2 digits

; Build sign list for each item in Buy menu
C3BCFD:  LDY #$AA8D      ; 7E/AA8D
         STY $2181       ; Set WRAM LBs
         LDX $00         ; Item slot: 1
C3BD07:  PHX             ; Save item slot
         LDA #$10        ; Signs left: 16
         STA $F3         ; Set counter
         JSR sk_slt6     ; Item in slot
         STA $9F         ; Memorize it
         JSR itmtype     ; Item's class
         STA $E0         ; Memorize it
         CMP #$06        ; Consumable?
         BEQ C3BF0A      ; Blank if so
         DEC A           ; Tool?
         BMI C3BF0A      ; Blank if so
         JSR itm_pw_     ; Item's power
         STA $9E         ; Memorize it

; Fork: Load address to actor's gear data
C3BD45:  LDX $00         ; 1st: Terra
C3BD47:  PHX             ; Save actor index
         JSR chk1EDC     ; Eligible char?
         BEQ C3BD87      ; Skip if not
         TDC             ; Clear A
         LDA $E0         ; Item class
         TAX             ; Index it
         LDA.L eqpadj-1,X; RAM adjustment
.adj     INY             ; Address +1
         DEC A           ; Adjustment -1
         BPL .adj        ; Loop till < 0

; Fork: Check if wearing shop item
         LDA $9F         ; Shop item
         CMP $001E,Y     ; In slot A?
         BEQ C3BD7E      ; Use "E" if so
         CMP $001F,Y     ; In slot B?
         BEQ C3BD7E      ; Use "E" if so

; Fork: Select sign based on item class
         TXA             ; Item class
         CMP #$05        ; Relic?
         BEQ nosign      ; Blank if so
         ASL A           ; Class x2
         TAX             ; Reindex it
         LDA $001E,Y     ; Item worn A
         JSR (sgnch-2,X) ; Select sign
         BRA setsign     ; Add to list

; Fork: Use "E" sign
C3BD7E:  LDA #$01        ; Sign: "E"
         BRA setsign     ; Add to list

; Fork: Use no sign
nosign:  TDC             ; Sign: None
setsign: STA $2180       ; Add to list
         DEC $F3         ; One less sign

; Fork: Handle next actor
C3BD87:  PLX             ; Actor index
         INX             ; Index +1
         INX             ; Index +1
         CPX #$0020      ; Done 16 chars?
         BNE C3BD47      ; Loop if not

; Fork: Blank remaining actor slots
C3BF0A:  STZ $2180       ; Blank sign
         DEC $F3         ; One less left
         BNE C3BF0A      ; Loop till last

; Fork: Handle next item
C3BD8F:  PLX             ; Item slot
         INX             ; Item slot +1
         CPX #$0008      ; Done 8 items?
         BNE C3BD07      ; Loop if not
         RTS

; Base gear slot for each item class
eqpadj:  db $00          ; Weapon
         db $03          ; Armor
         db $00          ; Shield
         db $02          ; Helmet
         db $04          ; Relic

; Jump table for item power checks
sgnch:   dw C3BDC8       ; Weapon
         dw C3BE8_       ; Armor
         dw C3BE58       ; Shield
         dw C3BE8_       ; Helmet

; Define sign by shop actor for weapon
C3BDC8:  CMP #$FF        ; R-Hand empty?
         BEQ C3BDE0      ; Branch if so
         JSR itmtype     ; Weapon class?
         BEQ C3BE86      ; Branch if so
C3BDE0:  LDA $001F,Y     ; L-Hand item
         CMP #$FF        ; None?
         BEQ C3BE9_      ; Use "^" if so

; Define sign by shop actor for shield
C3BE58:  JSR itmtype     ; Shield in R-Hand?
         BEQ C3BE86      ; Branch if so
         LDA $001F,Y     ; L-Hand item
         JSR itmtype     ; Shield class?
         BNE C3BE9_      ; Use "^" if not
         INY             ; Point to L-Hand
C3BE86:  LDA $001E,Y     ; Hand's item

; Compare actor's gear to shop item
C3BE8_:  CMP #$FF        ; Empty gear slot?
         BEQ C3BE9_      ; Use "^" if so
         JSR itm_pwr     ; Item's power
         CMP $9E         ; Matches shop's?
         BEQ C3BEAA      ; Use "=" if so
         BCS C3BEA2      ; Use "v" if above

; Fork: Use "^" sign
C3BE9_:  LDA #$02        ; Sign: "^"
         RTS

; Fork: Use "v" sign
C3BEA2:  LDA #$03        ; Sign: "v"
         RTS

; Fork: Use "=" sign
C3BEAA:  LDA #$04        ; Sign: "="
         RTS

; Count and draw quantity worn of item in Sell menu
C3BF66:  JSR C3BFCB      ; Selected item
C3BF69:  STA $E0         ; Memorize it
         STZ $65         ; Quantity worn: 0
         LDY #$184A      ; 7E/184A
         JSR C3D51B      ; Set SRAM pointer
         LDX #$001E      ; 1st: Guest 2
C3BF78:  JSR chk1EDC     ; Eligible char?
         BEQ C3BFA5      ; Skip if not
         LDA $0000,Y     ; Actor
         CMP #$0E        ; Guest?
         BCS C3BFA5      ; Skip if so
         LDY #$0005      ; 1st: Relic 2
C3BF97:  LDA [$E7],Y     ; Equipped item
         CMP $E0         ; Matches shop's?
         BNE C3BF9F      ; Branch if not
         INC $65         ; Quantity worn +1
C3BF9F:  DEY             ; Gear slot -1
         BPL C3BF97      ; Loop till R-Hand
C3BFA5:  REP #$21        ; 16-bit A; C-
         LDA $E7         ; SRAM pointer
         SBC #$0024      ; Actor data size
         STA $E7         ; Save changes
         SEP #$20        ; 8-bit A
         DEX             ; Actor index -1
         DEX             ; Actor index -1
         BPL C3BF78      ; Loop till < 0
         LDA $65         ; Quantity worn
         LDX #$7C3F      ; Text position
         JMP C304B_      ; Draw 2 digits

; Get menu entry designated by cursor
C3BFC2:  TDC             ; Clear A
         LDA $4B         ; Cursor slot
         JMP sk_slt5     ; Item in slot

; Get item in inventory slot
C3BFCB:  TDC             ; Clear A
         LDA $4B         ; List slot
         TAY             ; Index it
C3BFCF:  LDA $1869,Y     ; Item in slot
         CMP #$FF        ; Empty slot?
         RTS

; Set to condense BG3 text in shop menu
C3C018:  LDY #C3C037     ; C3/C037
C3C01B:  LDA #$C3        ; Bank: C3
         STA $4354       ; Set src HB
         LDA #$12        ; To $2112
         JMP dma5a       ; Set up HDMA

; Load item's name without ending string
C3C06_:  PHA             ; Save item
         LDX.W #!itm_ns  ; Letters: 13
         LDA.B #!itm_np!h; Text ptr HB
         LDY.W #!itm_np  ; Text ptr LBs
         JSR C3532Z      ; Set src, size
         PLA             ; Item
         JMP C3846_      ; Load name

; Compile and create actors and signs for Buy menu
C3C09A:  JSR C3C109      ; Compile actors
         LDX $00         ; List slot: 1
C3C0A2:  LDA $7E9E09,X   ; Actor in slot
         BMI C3C23B      ; Done if none
         PHX             ; Save slot
         PHA             ; Save actor
         LDY #C3C141     ; C3/C141
         JSR qanim2      ; Queue OAM fn
         TXY             ; Queue slot
         PLA             ; Actor
         STA $7E35C9,X   ; Link to OBJ
         JSR C378FA      ; Set anim ptr
         PLX             ; List slot
         LDA #$9C        ; Y: 156
         CPX #$0007      ; On upper row?
         BCC .sety       ; Branch if so
         LDA #$B8        ; Y: 184
.sety    STA $344A,Y     ; Set Y pos
         TXA             ; List slot
         SBC #$07        ; Row 2 column
         BCS .top        ; If lower row
         TXA             ; Row 1 column
.top     JSR x32         ; x32
         CLC             ; Prepare ADC
         ADC #$18        ; Add 24
         STA $33CA,Y     ; Set X pos
         JSR D8_35CA     ; Ptr HB: D8
         TDC             ; Zero A
         PHA             ; Put on stack
         PLB             ; Set DB to 0
         INX             ; List slot +1
         BRA C3C0A2      ; Do next char

; Fork: Create sign by each actor
C3C23B:  LDA #$0D        ; Char slot: 14
C3C23D:  PHA             ; Save char slot
         LDA #$04        ; Min slot: 32
         LDY #C3C253     ; C3/C253
         JSR C31173      ; Queue OAM fn
         PLA             ; Char slot
         STA $7E35C9,X   ; Assign to sign
         DEC A           ; Char slot -1
         BPL C3C23D      ; Loop till < 0
         RTS

; Compile actors and party members for Buy menu
C3C109:  JSR wm9E09      ; WRAM: 7E/9E09
         LDX $00         ; 1st: Terra
C3C111:  JSR chk1EDC     ; Eligible char?
         BEQ C3C132      ; Skip if not
         LDA $0000,Y     ; Actor
         CMP #$0E        ; Guest?
         BCS C3C132      ; Skip if so
         STA $2180       ; Add to list
C3C132:  INX             ; Actor index +1
         INX             ; Actor index +1
         CPX #$0020      ; Done 16 slots?
         BNE C3C111      ; Loop if not
         JSR C3C13B      ; End list

; Fork: Compile party members
C3C1CC:  LDX #$9E01      ; 7E/9E01
         STX $2181       ; Set WRAM LBs
         LDY $00         ; Member index: 0
C3C1D4:  LDA #$FF        ; Actor: None
         LDX $6D,Y       ; Actor's address
         BEQ C3C1E5      ; Blank if nobody
         LDA $00,X       ; Actor in slot
C3C1E5:  STA $2180       ; Add to list
         INY             ; Member index +1
         INY             ; Member index +1
         CPY #$0008      ; Done 4 members?
         BNE C3C1D4      ; Loop if not
         RTS

; Update shop actor
C3C141:  BNE C3C16A      ; If mode 1

; Mode 0: Initialize data
C3C149:  INC A           ; Hide actors: N
         TSB $47         ; Set menu flag
         REP #$20        ; 16-bit A
         LDA $32C9,X     ; Anim table ptr
         STA $34CA,X     ; Save original
         JSR C3120_      ; Set pose timer

; Fork: Create arrow under actor if in party
C3C1F0:  LDY #$0003      ; Member slot: 4
C3C1F4:  LDA $35C9,X     ; Shop actor
         CMP $9E01,Y     ; In member slot?
         BNE C3C234      ; Try next if not
         PHX             ; Save queue index
         PHY             ; Save member slot
         PHX             ; Save queue index
         LDY #C37B42     ; C3/7B42
         JSR qanim0      ; Queue OAM fn
         PLY             ; Actor's index
         LDA $33CA,Y     ; Actor's X
         STA $33CA,X     ; Set arrow's
         LDA $344A,Y     ; Actor's Y
         DEC A           ; +1 px up
         DEC A           ; +1 px up
         STA $344A,X     ; Set arrow's Y
         LDY #$EBA9      ; Anim ptr LBs
         LDA #$D8        ; Anim ptr HB
         JSR setanim     ; Set arrow's
         PLY             ; Member slot
         PLX             ; Queue index
C3C234:  DEY             ; Member slot -1
         BPL C3C1F4      ; Loop till < 0

; Mode 1: Sustain sprite
C3C16A:  LDA $47         ; Menu flags
         LSR A           ; Remove actors?
         BCC C3BCF9      ; Delete if so
         STZ $11D8       ; No Merit Award
         LDA $35C9,X     ; Actor
         JSR C3BCC_      ; Waving arms?
         LDX $2D         ; Queue index
         REP #$20        ; 16-bit A
         LDA $34CA,X     ; Base anim ptr
         BCC C3C190      ; If not waving
         ADC #$0008      ; Adjust pointer
C3C190:  STA $32C9,X     ; Set active ptr
         JMP C30860      ; Set OAM; requeue

; Check whether actor can equip shop item
C3BCC9:  STZ $11D8       ; No Merit Award
         LDA $35C9,X     ; Char list slot
         TAX             ; Index it
         LDA $9E09,X     ; Actor in slot
C3BCC_:  JSR C39C48      ; Define compat
         JSR C3BFC2      ; Item in slot
         JSR C38321      ; Compute index
C3BCF_:  REP #$21        ; 16-bit A; prefail
         LDA $D85001,X   ; Compatibility
         BIT $E7         ; Actor can use?
         SEP #$20        ; 8-bit A
         BEQ C3BCF9      ; Fail if not
C3BCF8:  SEC             ; Mark success
C3BCF9:  RTS

; Update shop sign
C3C253:  BNE C3C28B      ; If mode 1

; Mode 0: Initialize sprite
C3C25B:  LDA $35C9,X     ; Char slot
         TAX             ; Index it
         LDA $D8EBB1,X   ; X position
         STA $33CA,Y     ; Set sign's
         LDA $D8EBBF,X   ; Y position
         STA $344A,Y     ; Set sign's
         TYX             ; Queue index
         LDY #$EBCD      ; Anim ptr LBs
         LDA #$D8        ; Anim ptr HB
         JSR setanim     ; Set anim ptr
         REP #$20        ; 16-bit A
         TYA             ; Anim ptr LBs
         STA $34CA,X     ; Set original
         JSR C3120_      ; Set pose timer

; Mode 1: Sustain sprite
C3C28B:  LDA $47         ; Menu flags
         LSR A           ; Actors shown?
         BCC C3BCF9      ; Delete if not
         TDC             ; Clear A
         LDA $4B         ; Cursor slot
         JSR x16         ; x16
         ADC $35C9,X     ; Add char slot
         TAY             ; Index it
         JSR C3BCC9      ; Actor can equip?
         BCC C3BCF8      ; Exit if not
         LDA $AA8D,Y     ; Sign
         BEQ C3BCF8      ; Exit if none
         DEC A           ; Now 0-3
         XBA             ; Put in B
         LDA #$0C        ; Anim data size
         LDX $2D         ; Queue index
         JSR mul8        ; Sign x size
         REP #$21        ; 16-bit A; C-
         ADC $34CA,X     ; Add base ptr
         JMP C3C190      ; Set ptr; OAM

; Draw "Owned:" and "Equipped:" for Buy menu
C3C2E1:  LDY #C3C342     ; Text pointer
         JSR draw2c      ; Draw "Owned:"
         LDY #C3C34B     ; Text pointer
         JMP C3BB4C      ; Draw "Equipped:"

; =============================================================================

; Jump table for ending sequences
C3C406:  dw C3C5BE       ; 00: Fade screen before ending scene
         dw C3C5D4       ; 01: Return to event script
         dw C3C869       ; 02: Show aerial-view panning clouds
         dw C30000       ; 03: Unused...
         dw C3C8AD       ; 04: Show spinning clouds
         dw C3C5E1       ; 05: Start sideview airship scene
         dw C30000       ; 06: Unused...
         dw C30000       ; 07: Unused...
         dw C30000       ; 08: Unused...
         dw C3C794       ; 09: Start aerial-view airship scene
         dw C3C7D2       ; 0A: Show aerial-view airship
         dw C3C7E1       ; 0B: Fade screen to white
         dw C3C8D1       ; 0C: Start boat scene
         dw C3C917       ; 0D: Show boat sprite
         dw C30000       ; 0E: Unused...
         dw C30000       ; 0F: Unused...
         dw C3C9CD       ; 10: Start skimming airship scene
         dw C3CA8C       ; 11: Show swooping bird
         dw C30000       ; 12: Unused...
         dw C30000       ; 13: Unused...
         dw C3CD17       ; 14: Start frontview airship scene
         dw C3CDA0       ; 15: Show late bird flock
         dw C30000       ; 16: Unused...
         dw C30000       ; 17: Unused...
         dw C3CEA6       ; 18: Start aerial-view plains scene
         dw C30000       ; 19: Unused...
         dw C30000       ; 1A: Unused...
         dw C30000       ; 1B: Unused...
         dw C3CEEC       ; 1C: Start rearview airship scene
         dw C30000       ; 1D: Unused...
         dw C30000       ; 1E: Unused...
         dw C30000       ; 1F: Unused...
         dw C3D02F       ; 20: Start final airship scene
         dw C3D096       ; 21: Fade final airship
         dw C30000       ; 22: Unused...
         dw C30000       ; 23: Unused...
         dw C30000       ; 24: Unused...
         dw C30000       ; 25: Unused...
         dw C30000       ; 26: Unused...
         dw C30000       ; 27: Unused...
         dw C3E26E       ; 28: Start Shadow's scene
         dw C3E2B2       ; 29: Fade in "Shadow as"
         dw C3E2C1       ; 2A: Switch Shadow's names
         dw C3E2D0       ; 2B: Fade all but Shadow's name
         dw C30000       ; 2C: Unused...
         dw C3E6CE       ; 2D: Start first book scene
         dw C3E6DF       ; 2E: Animate first book
         dw C3E6FA       ; 2F: Pixelate and fade first book
         dw C30000       ; 30: Unused...
         dw C30000       ; 31: Unused...
         dw C3E2F8       ; 32: Start Cyan's scene
         dw C3E2B2       ; 33: Fade in "Cyan as"
         dw C3E2C1       ; 34: Switch Cyan's names
         dw C3E337       ; 35: Fade all but Cyan's name
         dw C30000       ; 36: Unused...
         dw C30000       ; 37: Unused...
         dw C3E2E7       ; 38: Fade actor's true name
         dw C30000       ; 39: Unused...
         dw C30000       ; 3A: Unused...
         dw C30000       ; 3B: Unused...
         dw C3E3E1       ; 3C: Start Mog's scene
         dw C3E2B2       ; 3D: Fade in "Mog as"
         dw C3E2C1       ; 3E: Switch Mog's names
         dw C3E2D0       ; 3F: Fade all but Mog's name
         dw C3E34B       ; 40: Start Edgar and Sabin's scene
         dw C3E2B2       ; 41: Fade in "Edgar as"
         dw C3E373       ; 42: Switch Edgar's names
         dw C3E382       ; 43: Show Sabin's name, fade Edgar's
         dw C3E3A2       ; 44: Delete Edgar's name
         dw C3E3B2       ; 45: Switch Sabin's names
         dw C3E432       ; 46: Start Gogo's scene
         dw C3E2B2       ; 47: Fade in "Gogo as"
         dw C3E2C1       ; 48: Switch Gogo's names
         dw C3E337       ; 49: Fade all but Gogo's name
         dw C3E711       ; 4A: Start last book scene
         dw C3E736       ; 4B: Stop last book and fade in text
         dw C3E749       ; 4C: Pixelate and fade last book
         dw C30000       ; 4D: Unused...
         dw C30000       ; 4E: Unused...
         dw C3E337       ; 4F: Fade all but Sabin's name
         dw C3E4FA       ; 50: Start Gau's scene
         dw C3E2B2       ; 51: Fade in "Gau as"
         dw C3E522       ; 52: Switch Gau's names; create face
         dw C3E2D0       ; 53: Fade all but Gau's name
         dw C30000       ; 54: Unused...
         dw C30000       ; 55: Unused...
         dw C30000       ; 56: Unused...
         dw C30000       ; 57: Unused...
         dw C30000       ; 58: Unused...
         dw C30000       ; 59: Unused...
         dw C3E54B       ; 5A: Start Terra's scene
         dw C3E2B2       ; 5B: Fade in "Terra as"
         dw C3E573       ; 5C: Switch Terra's names, create glint
         dw C3E2D0       ; 5D: Fade all but Terra's name
         dw C30000       ; 5E: Unused...
         dw C30000       ; 5F: Unused...
         dw C30000       ; 60: Unused...
         dw C30000       ; 61: Unused...
         dw C30000       ; 62: Unused...
         dw C30000       ; 63: Unused...
         dw C3E59C       ; 64: Start Locke and Celes's scene
         dw C3E2B2       ; 65: Fade in "Locke as"
         dw C3E373       ; 66: Switch Locke's names
         dw C3E5D5       ; 67: Show Celes's name, fade Locke's
         dw C3E3A2       ; 68: Delete Locke's name
         dw C3E605       ; 69: Switch Celes's names
         dw C3E2D0       ; 6A: Fade all but Celes's name
         dw C30000       ; 6B: Unused...
         dw C30000       ; 6C: Unused...
         dw C30000       ; 6D: Unused...
         dw C3E635       ; 6E: Start Relm's scene
         dw C3E2B2       ; 6F: Fade in "Relm as"
         dw C3E2C1       ; 70: Switch Relm's names
         dw C3E2D0       ; 71: Fade all but Relm's name
         dw C30000       ; 72: Unused...
         dw C30000       ; 73: Unused...
         dw C30000       ; 74: Unused...
         dw C30000       ; 75: Unused...
         dw C30000       ; 76: Unused...
         dw C30000       ; 77: Unused...
         dw C3E686       ; 78: Start Strago's scene
         dw C3E2B2       ; 79: Fade in "Strago as"
         dw C3E2C1       ; 7A: Switch Strago's names
         dw C3E337       ; 7B: Fade all but Strago's name
         dw C30000       ; 7C: Unused...
         dw C30000       ; 7D: Unused...
         dw C30000       ; 7E: Unused...
         dw C30000       ; 7F: Unused...
         dw C30000       ; 80: Unused...
         dw C30000       ; 81: Unused...
         dw C3E75D       ; 82: Start Setzer's scene
         dw C3E2B2       ; 83: Fade in "Setzer as"
         dw C3E2C1       ; 84: Switch Setzer's names
         dw C3E7B2       ; 85: Fade background
         dw C3E7C1       ; 86: Fade cards and name
         dw C3E7E5       ; 87: Start Umaro's scene
         dw C3E2B2       ; 88: Fade in "Umaro as"
         dw C3E2C1       ; 89: Switch Umaro's names
         dw C3E2D0       ; 8A: Fade all but Umaro's name

; Display requested ending cinematic
C3C51C:  JSR C300C0      ; Reroute NMI, etc.
         JSR C368FA      ; Reset variables
         LDY #$012C      ; Scroll delay: 300
         STY $85         ; For true char name
         STZ $B4         ; Blue credits: N
         LDY #$7FFF      ; $7FFF
         STY $14         ; Set VRAM ptr
         INC $12         ; DMA size LB: 1
         STZ $13         ; DMA size HB: 0
         JSR C3352F      ; Disable pad, etc.
         JSL $D4CD48     ; Init registers
         JSR C3DD73      ; Clear VRAM
;        LDY $00         ; Scene timer: 0
         STY $CF         ; Set for text
         STY $20         ; Set for sequence
         JSL $D4CF3B     ; Back up data
         LDA $0201       ; Event parameter
         STA $26         ; Set scene mode
         CMP #$28        ; Mode 7 scene?
         BCS .non_m7     ; Branch if not
         LDY #$0100      ; Base divisor
         STY $C5         ; Set matrices'
;        LDY #$0100      ; 256
         STY $BF         ; Set center X
         LDY #$0080      ; 128
         STY $C1         ; Set center Y
         STY $35         ; Set BG1 X-Pos
         LDY $00         ; Vertical angle
         STY $C7         ; Set for BG1
;        LDY $00         ; Mod index: 0
         STY $C3         ; Set matrices'
         JSR C3C751      ; Load OBJ colors
         JSR C3D173      ; Load BG1 colors
         LDY #$0040      ; Palettes: 2
         LDX #$31C9      ; To OBJ pal 4
         JSR C3A79_      ; Make text black
         JSL $D4CED7     ; Init registers
.non_m7  JSR C3C5A5      ; Handle scene
         JSL $D4CF4B     ; Restore backup
         JSR C3352F      ; Disable pad, etc.
         RTL

; Enable color addition on BG1
C3C59F:  LDA #$01        ; BG1: ADD mode
         STA $2131       ; Set color math
C3C5A4:  RTS

; Handle ending sequence
C3C5A5:  TDC             ; Clear A
         LDA $26         ; Scene mode
         INC A           ; Exit to map?
         BEQ C3C5A4      ; Exit if so
         REP #$20        ; 16-bit A
         ASL A           ; Double it
         TAX             ; Index it
         SEP #$20        ; 8-bit A
         JSR (C3C406-2,X); Handle mode
         JSR C311B0      ; Run anim queue
         JSR C31368      ; Refresh screen
         BRA C3C5A5      ; Loop

; Load non-text sprite colors for Mode 7 scene
C3C751:  LDY #$3149      ; To OBJ pal 0
         LDX #$97F4      ; Source LBs
         JSR pal_c2      ; Do balloon's
         LDY #$3169      ; To OBJ pal 1
         LDX #$9794      ; Source LBs
         JSR pal_c2      ; Do airships'
         LDY #$3189      ; To OBJ pal 2
         LDX #$97B4      ; Source LBs
         JSR pal_c2      ; Do boat's
         LDY #$31A9      ; To OBJ pal 3
         LDX #$97D4      ; Source LBs
         JSR pal_c2      ; Do cockpit's
         LDY #$3209      ; To OBJ pal 6
         LDX #$9834      ; Source LBs
         JSR pal_c2      ; Do gray birds'
         LDY #$3229      ; To OBJ pal 7
         LDX #$9814      ; Source LBs
         JMP pal_c2      ; Do spray's

; 00: Fade screen before ending scene
C3C5BE:  LDY $20         ; Delay expired?
         BNE C3C5A4      ; Exit if not
         LDA #$0F        ; Frames: 15
;        STA $20         ; Set exit delay
         JSR C3E3A_      ; Next: Exit
         LDY #C3D1FD     ; C3/D1FD
         JMP qanim0      ; Queue fade-out

; 01: Return to event script after delay
C3C5D4:  LDY $20         ; Delay expired?
         BNE C3C5A4      ; Exit if not
         DEY             ; Null value
         STY $26         ; Next: Exit
         RTS

; 05: Start sideview airship scene
C3C5E1:  INC $B4         ; Blue text: Y
         JSR C3D439      ; Upload font GFX
         JSR C3D6B1      ; Unpack map, GFX
         JSR C3D66B      ; Pal numbers loc
         JSR C3D552      ; Convert M7 GFX A
         JSR C3D42A      ; Insert tilemap A
         JSR C3D634      ; Upload M7 map A
         JSR C3D562      ; Convert M7 GFX B
         JSR C3D42A      ; Insert tilemap B
         JSR C3D64C      ; Upload M7 map B
         JSL $D4CBFC     ; Queue HDMA 1-7
         LDY #$0010      ; Zoom-out: 16
         STY $8E         ; Set for airship
         JSR db7e_       ; Set DB to 7E
         LDY #$FFF1      ; H-Scroll: -15
         STY $B68D       ; Set for airship
         LDY #$002E      ; V-Scroll: 46
         STY $B68F       ; Set for airship
         LDY #$0071      ; Center X: 113
         STY $B695       ; Set for airship
         LDY #$008D      ; Center Y: 141
         STY $B697       ; Set for airship
         PLB             ; Restore DB
         LDY #$5E2F      ; Clouds' angle
         STY $C6         ; Matrix divisor
         LDY #$0311      ; Center Y: 785
         STY $C1         ; Set for clouds
         LDY #$0291      ; BG1's Y: 657
         STY $37         ; Set for clouds
         JSR C3DDD2      ; Make matrix tbl
         LDX #$9DC0      ; Src: C2/9DC0
         LDA #$01        ; Sprites: 1
         JSR C3D9F1      ; Do roles OAM
         LDX #$9C44      ; Src: C2/9C44
         LDA #$02        ; Sprites: 2
         JSR C3D9FA      ; Do staff OAM
         LDY #C3D7B9     ; C3/D7B9
         JSR qanim2      ; Queue credits
         LDY #C3C681     ; C3/C681
         JSR qanim2      ; Queue M7 anim
         LDY #C3D0F2     ; C3/D0F2
         JSR qanim0      ; Queue rotor anim
         JSR C3D018      ; Load sky/plane's
         LDY #$021C      ; Frames: 540
C3C66_:  STZ $26         ; Next: Fade-out
C3C667:  STY $20         ; Set fade delay
C3C66C:  STZ $44         ; Brightness: 0
         LDY #C3D1CC     ; C3/D1CC
         JSR qanim0      ; Queue fade-in
         LDA #$0F        ; Frames: 15
         JMP C3DFFB      ; Set fade timer

; 02: Show aerial-view panning clouds
C3C869:  LDA #$F0        ; Fade delay: 240
         LDY #$8B3C      ; Cloud anim ptr
C3C87E:  STA $20         ; Set fade delay
         JSR C3D225      ; Queue cloud anim
         JSR C3D522      ; Upload GFX, map
         JSL $D4CB8F     ; HDMA: Matrices
         LDA #$02        ; Vertical angle
         STA $C8         ; Set for clouds
         LDA #$F0        ; Base divisor
         JSR C3DDDY      ; Make matrix tbl
         JSR C3D2A0      ; Queue M7 handler
         STZ $26         ; Next: Fade-out
         BRA C3C66C      ; Queue fade-in

; 09: Start aerial-view airship scene
C3C794:  INC $B4         ; Blue text: Y
         JSR C3D522      ; Upload GFX, map
         JSL $D4CB8F     ; HDMA: Matrices
         LDY #$8A70      ; Anim ptr LBs
         JSR C3D225      ; Queue cloud anim
         LDX #$9DC4      ; Src: C2/9DC4
         LDA #$0A        ; Sprites: 10
         JSR C3D9F1      ; Do roles OAM
         LDX #$9C4C      ; Src: C2/9C4C
         LDA #$18        ; Sprites: 24
         JSR C3D9FA      ; Do staff OAM
         LDY #C3D7CB     ; C3/D7CB
         JSR qanim2      ; Queue credits
         JSR C3D2A0      ; Queue M7 handler
         LDY #$03C0      ; Frames: 960
;        STY $20         ; Set plane delay
C3C79_:  INC $26         ; Next: C3/C7D2
         BRA C3C667      ; Queue fade-in

; Shrink sideview airship and scroll clouds
C3C681:  REP #$20        ; 16-bit A
         LDA $23         ; NMI count
         LSR A           ; Odd value?
         BCS C3C6A6      ; Branch if so
         INC $8E         ; Matrix A +1
C3C6A6:  LDA $8E         ; Matrix A
         STA $0600       ; Set for airship
         STZ $07C2       ; Matrix B: 0
         STZ $0984       ; Matrix C: 0
         DEC $35         ; BG1's X -1
         DEC $BF         ; Center X -1
         LDX #$0002      ; First: Y
.loop    LDA $35,X       ; BG1 X or Y
         STA $B691,X     ; To HDMA table
         LDA $BF,X       ; Center X or Y
         STA $B699,X     ; To HDMA table
         DEX             ; Index -1
         DEX             ; Index -1
         BPL .loop       ; Loop till < 0
         SEC             ; Set to requeue
C3C6B9:  RTS

; 0A: Show aerial-view airship and its shadow
C3C7D2:  LDY $20         ; Delay expired?
         BNE C3C6B9      ; Exit if not
         INC $26         ; Next: C3/C7E1
         LDY #$0870      ; Frames: 2160
         STY $20         ; Set fade delay

; Fork: Create airship and its shadow
C3C802:  JSR C3C846      ; Queue ship fn
         LDY #$8CDC      ; Ship anim ptr
         JSR C3C84F      ; Set object data
         LDY #C3DE84     ; C3/DE84
         JSR qanim3      ; Queue shadow fn
         LDY #$8CF0      ; Shadow anim ptr
         JSR C3C84F      ; Set object data
         LDA #$B0        ; Y: 176
         JMP C3EA0Z      ; Set shadow's

; Initialize data for aerial-view airship or its shadow
C3C84F:  REP #$20        ; 16-bit A
         LDA #$FFC0      ; H-Spd: -0.25 px
         STA $7E34C9,X   ; Set sprite's
         LDA #$FFE0      ; V-Spd: -0.125 px
         STA $7E3549,X   ; Set sprite's
         LDA #$0400      ; Frames: 1024
         JSR C3DFFB      ; Set hide delay
         PEA $A0F8       ; Pos: 248, 160
         PHY             ; Anim ptr LBs
         JMP C3E8AF      ; Set object data

; 0B: Fade screen to white
C3C7E1:  LDY $20         ; Delay expired?
         BNE C3C6B9      ; Exit if not
         LDA #$C3        ; Bank: C3
         STA $ED         ; Set src HB
         LDA #$02        ; Slowness: 2
         LDY #$30A9      ; To BG pal 3
         LDX #C310D4-2   ; White palette
         JSR C30FAA      ; Whiten clouds
C3C7E_:  STZ $26         ; Next: Black out
         JMP C3E5EF      ; Set fade delay

; Queue linearly moving temporary sprite
C3C846:  LDY #C3DE84     ; C3/DE84
         JMP qanim1      ; Queue OAM fn

; 04: Show spinning clouds
C3C8AD:  LDA #$78        ; Fade delay: 120
         LDY #$8B42      ; Cloud anim ptr
         JSR C3C87E      ; Load map, etc.
         LDA #$80        ; Vertical angle
         STA $C8         ; Set for clouds
;        LDA #$80        ; Base divisor
         JMP C3DDDY      ; Make matrix tbl

; 0C: Start boat scene
C3C8D1:  JSR C3C59F      ; Color math: BG1
         JSR C3D57_      ; Upload font, etc.
         JSR C3D664      ; Pal numbers loc
         JSR C3D552      ; Convert M7 GFX A
         JSR C3D5CB      ; Insert tilemap A
         JSR C3D634      ; Upload M7 map A
         JSR C3D562      ; Convert M7 GFX B
;        JSR C3D5CB      ; Insert tilemap B
         JSR C3D64C      ; Upload M7 map B
         JSL $D4CB8F     ; HDMA: Matrices
         LDA #$10        ; Base divisor
         STA $C6         ; Set matrices'
         LDY #$8AD9      ; Anim ptr LBs
         JSR C3D225      ; Queue sea anim
         LDX $00         ; OBJ data index
         JSR C3CAC7      ; Create birds
         LDX #$9DEC      ; Src: C2/9DEC
         LDA #$06        ; Sprites: 6
         JSR C3D9F1      ; Do roles OAM
         LDX #$9CAC      ; Src: C2/9CAC
         LDA #$10        ; Sprites: 16
         JSR C3D9FA      ; Do staff OAM
         LDY #C3D819     ; C3/D819
         JSR qanim2      ; Queue credits
         JSR C3D2A0      ; Queue M7 handler
         LDY #$0438      ; Delay: 1080
;        STY $20         ; Set boat OBJ's
;        INC $26         ; Next: C3/C917
         JMP C3C79_      ; Queue fade-in

; Scroll sky eastward
C3CAA2:  LDA $23         ; NMI count
         LSR A           ; Odd value?
         BCS C3CABA      ; Exit if so
         REP #$20        ; 16-bit A
         DEC $39         ; BG2 camera -1
         BRA C3CABA      ; 8-bit A; requeue

; Scroll sky westward
C3CAB0:  LDA $23         ; NMI count
         LSR A           ; Odd value?
         BCS C3CABA      ; Exit if so
         REP #$20        ; 16-bit A
         INC $39         ; BG2 camera +1
C3CABA:  SEC             ; Set to requeue
C3CABC:  RTS

; 0D: Show boat sprite
C3C917:  LDY $20         ; Delay expired?
         BNE C3CABC      ; Exit if not
         STZ $26         ; Next: Fade-out
         LDY #$02D0      ; Frames: 720
         STY $20         ; Set fade delay

; Fork: Create boat sprite
C3C94E:  LDY #C3C9A2     ; C3/C9A2
         JSR qanim1      ; Queue R-Half
         JSR C3C94_      ; Set pos, etc.
         LDY #$F7FD      ; Anim table ptr
         JSR setaniX     ; Set R-Half's
         JSR C3E9D2      ; Queue L-Half
C3C94_:  REP #$20        ; 16-bit A
         LDA #$FFC0      ; H-Spd: -0.25 px
         STA $7E34C9,X   ; Set L-Half's
         PEA $50F8       ; Pos: 248, 80
         PEA $F809       ; Anim table ptr
         JMP C3EA0V      ; Set object data

; Animate boat's right half
C3C9A2:  BNE C3C9BC      ; If mode 1

; Mode 0: Initialize timers
C3C9AA:  LDA #$40        ; Frames: 64
         STA $3349,X     ; Set show delay
;        STZ $334A,X     ; Clear its HB
         JSR C3120_      ; Set pose timer

; Mode 1: Sustain sprite
C3C9BC:  LDY $3349,X     ; Delay expired?
         BNE C3CFE5      ; Branch if not
         JMP C3EBD0      ; Scroll & OAM

; Decrease sprite's animation mode timer
C3CFE5:  REP #$20        ; 16-bit A
C3CFE_:  LDX $2D         ; Queue index
         JMP C3DF59      ; Timer -1; requeue

; 10: Start skimming airship scene
C3C9CD:  JSR C3D57_      ; Upload font, etc.
         JSR C3D664      ; Pal numbers loc
         JSR C3D552      ; Convert M7 GFX A
         JSR C3D5FB      ; Insert tilemap A
         JSR C3D634      ; Upload M7 map A
         JSR C3D562      ; Convert M7 GFX B
;        JSR C3D5FB      ; Insert tilemap B
         JSR C3D64C      ; Upload M7 map B
         JSR C3D36F      ; Load sky/gradient
         LDA #$28        ; Y: 40
         STA $3B         ; Set BG2 Y-Pos
         LDX #$FF6D      ; Mod index: 109
;        STX $C3         ; Set matrices'
         LDY #$034C      ; Vertical angle
;        STY $C7         ; Set for sea
         LDA #$FD        ; Base divisor
         JSR C3DDDX      ; Make matrix tbl
         LDX #$0018      ; OBJ data index
         JSR C3CAC7      ; Create birds
         LDY #C3CBC6     ; C3/CBC6
         JSR qanim2      ; Queue airship
         LDY #$8BB1      ; Anim ptr LBs
         LDA #$C2        ; Anim ptr HB
         JSR setanim     ; Set airship's
         JSR C3E9D2      ; Queue shadow
         LDY #$8BEC      ; Anim ptr LBs
         LDA #$C2        ; Anim ptr HB
         JSR setanim     ; Set shadow's
         LDA #$80        ; Coord: 128
         JSR C30940      ; Set shadow's X
         JSR C3EA0Z      ; Set shadow's Y
         LDX #$9E04      ; Src: 9E04
         LDA #$08        ; Sprites: 8
         JSR C3D9F1      ; Do roles OAM
         LDX #$9CEC      ; Src: C2/9CEC
         LDA #$19        ; Sprites: 25
         JSR C3D9FA      ; Do staff OAM
         LDY #C3D849     ; C3/D849
         JSR qanim2      ; Queue credits
         LDY #$8B36      ; Anim ptr LBs
         JSR C3D225      ; Queue sea anim
         LDY #C3CAA2     ; C3/CAA2
         JSR qanim3      ; Set to pan sky
         JSR C3D2A0      ; Queue M7 handler
         LDY #$04B0      ; Frames: 1200
;        STY $20         ; Set bird's delay
;        INC $26         ; Next: Late bird
         JMP C3C79_      ; Queue fade-in

; Load cloudy sky graphics data, exclude sky from Mode 7
C3CA71:  LDY #$3049      ; To BG pal 0
         LDX #$953C      ; Source LBs
         JSR pal_c2      ; Load sky colors

; Fork: Unpack and upload sky graphics and tilemap
C3D4D3:  REP #$20        ; 16-bit A
         LDA $EEB209     ; Tilemap loc
         PHA             ; Memorize it
         LDA $EEB206     ; Source LBs
         TAX             ; Put in X
         SEP #$20        ; 8-bit A
         LDA $EEB208     ; Source HB
         JSR C3DD61      ; Unpack graphics
         LDX #$1A20      ; Bytes: 6688
         LDY #$4000      ; VRAM pointer
         JSR C3DDB_      ; Upload graphics
         PLX             ; Source LBs
         LDA $EEB20B     ; Source HB
         JSR C3DD61      ; Unpack tilemap
         LDA #$20        ; Add: Priority
         STA $EE         ; Set complement
         LDX #$0400      ; Bytes: 1024
         LDY #$5000      ; VRAM pointer
         JSR C3DDB9      ; Upload tilemap

; Fork: Configure Mode 7 not to apply to cloudy sky
C3D34B:  JSL $D4CB8F     ; HDMA: Matrices
         STZ $4310       ; 1Rx1B to PPU
         LDA #$05        ; $2105
         STA $4311       ; To BG mode
         LDY #C3D36A     ; C3/D36A
         STY $4312       ; Set src LBs
         LDA #$C3        ; Bank: C3
         STA $4314       ; Set src HB
         LDA #$02        ; Channel: 1
         JMP C3D38_      ; Queue HDMA-1

; 11: Show swooping bird
C3CA8C:  LDY $20         ; Delay expired?
         BNE C3CAFC      ; Exit if not
         STZ $26         ; Next: Fade-out
         LDY #$0708      ; Frames: 1800
         STY $20         ; Set fade delay
         LDY #C3CC79     ; C3/CC79
         JMP qanim1      ; Queue bird OAM

; Create 6 oscillating birds
C3CAC7:  LDA #$06        ; Birds: 6
C3CAC9:  PHA             ; Save counter
         PHX             ; Save ROM index
         LDY #C3CB68     ; C3/CB68
         JSR qanim2      ; Queue OAM fn
         LDA $26         ; Current scene
         CMP #$0C        ; Boat scene?
         BNE .skip       ; Branch if not
         JSR C3EA6C      ; Follow BG1: Y
.skip    TXY             ; Queue index
         PLX             ; ROM index
         PHB             ; Save DB
         JSR db7e        ; Set DB to 7E
         REP #$20        ; 16-bit A
         LDA $CFF7B5,X   ; Anim table ptr
         INX             ; ROM index +1
         INX             ; ROM index +1
         JSR C37906      ; Set bird's
         LDA $CFF7B5,X   ; Base X / 2
         ASL A           ; Double it
         INX             ; ROM index +1
         STA $34CA,Y     ; Set bird's
         LDA $CFF7B5,X   ; Base Y
         INX             ; ROM index +1
         STA $344A,Y     ; Set bird's
         PLB             ; Restore DB
         PLA             ; Birds left
         DEC A           ; One less left
         BNE C3CAC9      ; Loop till last
C3CAFC:  RTS

; Animate oscillating bird
C3CB68:  BNE C3CB87      ; If mode 1

; Mode 0: Initialize data
C3CB70:  LDA.L C3CB68,X  ; X-Mod index
         STA $3349,X     ; Set bird's
         LDA #$CF        ; Bank: CF
         STA $35CA,X     ; Set ptr HB
         JSR C3120_      ; Set pose timer

; Mode 1: Update back-and-forth motion (range: -8 to +7)
C3CB87:  LDA $3349,X     ; X-Mod index
         JSR C3DE76      ; Fract X mod
         JSR abs16       ; |Mod| / 16
C3CBBA:  STA $33C9,X     ; Init bird's X
         SEP #$20        ; 8-bit A
         INC $3349,X     ; X-Mod index +1
         BRA C3CBFA      ; Scroll & OAM

; Animate skimming airship
C3CBC6:  BNE C3CBE6      ; If mode 1

; Mode 0: Initialize data
C3CBCE:  STZ $3349,X     ; Anim timer: 0
         LDA #$78        ; X: 120
         STA $33CA,X     ; Set sprite's
         LDA #$20        ; Base Y/2: 32
         STA $354A,X     ; Set sprite's
         JSR C3120_      ; Set pose timer

; Mode 1: Sustain sprite
C3CBE6:  JSR C3CBFF      ; Init Y position
         INC $3349,X     ; Anim timer +1
         LDA $3349,X     ; Anim timer
         CMP #$38        ; Within 56-255?
         BCS C3CBFA      ; Branch if so
         JSR C3CC38      ; Create spray
C3CBFA:  JMP C3EBD0      ; Scroll & OAM

; Update airship's up-and-down motion (range: -8 to +7)
C3CBFF:  REP #$20        ; 16-bit A
         LDA $3349,X     ; Anim timer
         JSR C3DE7A      ; Fract Y mod
         JSR abs16       ; |Mod| / 16
C3CC25:  CLC             ; Prepare ADC
         ADC $3549,X     ; Add base Y
         STA $3449,X     ; Init ship's Y
         SEP #$20        ; 8-bit A
         RTS

; Create water spray
C3CC38:  LDA $23         ; NMI count
         AND #$03        ; Now 0-3
         BNE C3CC37      ; Exit if 1-3
         JSR C3C846      ; Queue anim fn
         LDA #$18        ; Frames: 24
         STA $3349,X     ; Set hide delay
         PEA $7C7E       ; Pos: 126, 124
         PEA $8D0B       ; Anim table ptr
         REP #$20        ; 16-bit A
         LDA #$0100      ; H-Spd: +1 px
         STA $34C9,X     ; Set sprite's
         LDA #$FF80      ; V-Spd: -0.5 px
         STA $3549,X     ; Set sprite's
         JMP C3E8AF      ; Set object data

; Divide A by 32
div32:   LSR A           ; ÷2

; Divide sprite's position modifier by 16
C3CC33:  LSR A           ; ÷2
         LSR A           ; ÷4
         LSR A           ; ÷8
         LSR A           ; ÷16
C3CC37:  RTS

; Animate swooping bird
C3CC79:  BNE C3CCA8      ; If mode 1

; Mode 0: Initialize sprite
C3CC81:  LDA #$10        ; Pos index: 16
         STA $3349,X     ; Set sprite's
         LDY #$F772      ; Anim ptr LBs
;        LDA #$CF        ; Anim ptr HB
         JSR C3EBD_      ; Set pose timer

; Mode 1: Sustain sprite
C3CCA8:  LDA $33CA,X     ; X position
         CMP #$08        ; Under 8?
         BCC C3CC37      ; Delete if so

; Fork: Set X position
C3CCB3:  LDA $3349,X     ; Position index
         JSR C3DE76      ; Fractional X
         ASL A           ; Double it
         STA $33C9,X     ; Set bird's X

; Fork: Set Y position
C3CCE2:  LDA $3349,X     ; Position index
         JSR C3DE7A      ; Fractional Y
         STA $3449,X     ; Set bird's Y
;        INC $3349,X     ; Pos index +1
         JMP C3D010      ; Set OAM; requeue

; 14: Start frontview airship scene
C3CD17:  JSR C3D573      ; Upload GFX, map
         JSR C3D36F      ; Load sky/gradient
         LDA #$28        ; Y: 40
         STA $3B         ; Set BG2 Y-Pos
         LDX #$FFCD      ; Mod index: 205
;        STX $C3         ; Set matrices'
         LDY #$030B      ; Vertical angle
;        STY $C7         ; Set for field
         LDA #$F0        ; Base divisor
         JSR C3DDDX      ; Make matrix tbl
         LDX #$0030      ; OBJ data index
         JSR C3CAC7      ; Create birds
         PEA $8C25       ; Anim table ptr
         PEA $5878       ; Pos: 120, 88
         JSR C3CFDC      ; Queue airship
         LDY #$8B39      ; Anim ptr LBs
         JSR C3D225      ; Queue field anim
         LDX #$9E24      ; Src: C2/9E24
         LDA #$09        ; Sprites: 9
         JSR C3D9F1      ; Do roles OAM
         LDX #$9D50      ; Src: C2/9D50
         LDA #$1C        ; Sprites: 28
         JSR C3D9FA      ; Do staff OAM
         LDY #C3D897     ; C3/D897
         JSR qanim2      ; Queue credits
         LDY #C3CAB0     ; C3/CAB0
         JSR qanim3      ; Set to pan sky
         JSR C3D2A0      ; Queue M7 handler
         LDY #$04B0      ; Frames: 1200
;        STY $20         ; Set flock delay
;        INC $26         ; Next: Late flock
         JMP C3C79_      ; Queue fade-in

; 15: Show late bird flock
C3CDA0:  LDX $20         ; Delay expired?
         BNE C3CDE9      ; Exit if not
         STZ $26         ; Next: Fade-out
         LDY #$0708      ; Frames: 1800
         STY $20         ; Set fade delay
         JSR db7e_       ; Set DB to 7E
;        TAX             ; Bird index: 0
.loop    PHX             ; Save bird index
         LDY #C3CDEA     ; C3/CDEA
         JSR qanim2      ; Queue bird fn
         TXY             ; Queue index
         PLX             ; Bird index
         REP #$20        ; 16-bit A
         LDA.L birdpos,X ; Bird position
         PHX             ; Save bird index
         TYX             ; Queue index
         JSR birdset     ; Set object data
;        STZ $35C9,X     ; Course: NE
         JSR C31206      ; Set pose timer
         JSR C3CE43      ; Define path
         PLX             ; Bird index
         INX             ; Bird index +1
         INX             ; Bird index +1
         CPX #$0008      ; Done all 4?
         BNE .loop       ; Loop if not
         PLB             ; Restore DB
C3CDE9:  RTS

; Set object data for the above
birdset: PHA             ; Save position
         PEA $F74F       ; Anim table ptr
         JMP C3EA0V      ; Set object data

; Bird positions for the above
birdpos: dw $5000        ; A: 0, 80
         dw $4018        ; B: 24, 64
         dw $6840        ; C: 64, 104
         dw $4810        ; D: 16, 72

; Animate bird from late flock
C3CDEA:  CMP #$0C        ; At final mode?
         BEQ C3CE33      ; Branch if so

; Modes 0-5: Alter course
C3CE1F:  LDY $3349,X     ; Mode expired?
         BNE C3CE2B      ; Branch if not
         INC $3649,X     ; Mode +1
         JSR C3CE43      ; Alter path
C3CE2B:  JMP C3DC9B      ; Scroll/OAM/timer

; Mode 6: Delete if set to wrap horizontally
C3CE33:  JSR C3DEC0      ; Scroll & OAM
         LDA $33CA,X     ; Bird's X
         CMP #$01        ; Dequeue if 0
         RTS

; Alter course of bird from late flock
C3CE43:  TXY             ; Queue index
         TDC             ; Clear A
         LDA $35C9,Y     ; Path index
         INC $35C9,X     ; Path Index +1
         ASL A           ; Index x2
         TAX             ; Index it
         REP #$20        ; 16-bit A
         LDA.L C3CE6E,X  ; H-Speed
         STA $34C9,Y     ; Set bird's
         LDA.L C3CE70,X  ; V-Speed
         STA $3549,Y     ; Set bird's
         LDA.L C3CE72,X  ; Path timer
         STA $3349,Y     ; Set bird's
         SEP #$20        ; 8-bit A
         RTS

; Late bird flock's course
C3CE6E:  dw $0020        ; H-Speed: +0.125 px
         dw $0020        ; H-Speed: +0.125 px
         dw $0020        ; H-Speed: +0.125 px
         dw $0010        ; H-Speed: +0.0625 px
         dw $FFE0        ; H-Speed: -0.125 px
         dw $FFC0        ; H-Speed: -0.25 px
         dw $FF80        ; H-Speed: -0.5 px
C3CE70:  dw $FFF0        ; V-Speed: -0.0625 px
         dw $0000        ; V-Speed: 0
         dw $0020        ; V-Speed: +0.125 px
         dw $0040        ; V-Speed: +0.25 px
         dw $0040        ; V-Speed: +0.25 px
         dw $0040        ; V-Speed: +0.25 px
         dw $0080        ; V-Speed: +0.5 px
C3CE72:  dw $0384        ; Frames: 900
         dw $003C        ; Frames: 60
         dw $003C        ; Frames: 60
         dw $003C        ; Frames: 60
         dw $003C        ; Frames: 60
         dw $00B4        ; Frames: 180
         dw $00B4        ; Frames: 180

; 18: Start aerial-view plains scene
C3CEA6:  JSR C3C59F      ; Color math: BG1
         JSR C3D573      ; Upload GFX, map
         JSL $D4CB8F     ; HDMA: Matrices
         LDA #$18        ; Base divisor
         JSR C3DDDY      ; Make matrix tbl
         LDY #$8AFA      ; Anim ptr LBs
         JSR C3D225      ; Queue field anim
         LDX #$9E48      ; Src: C2/9E48
         LDA #$39        ; Sprites: 57
         JSR C3D9F1      ; Build text OAM
         LDY #C3D8E5     ; C3/D8E5
         JSR qanim2      ; Queue credits
         JSR C3D2A0      ; Queue M7 handler
         LDY #$0E10      ; Frames: 3600
;        STY $20         ; Set fade delay
;        STZ $26         ; Next: Fade-out
         JMP C3C66_      ; Queue fade-in

; 1C: Start rearview airship scene
C3CEEC:  JSR C3D573      ; Upload GFX, map
         LDY #$D7BA      ; Src: 7E/D7BA
         JSR C3D51B      ; Define source
         LDX #$0580      ; Bytes: 1408
         LDY #$6C00      ; VRAM pointer
         JSR C3DDB_      ; Upload balloon
         JSR C3D36F      ; Load sky/gradient
         LDY #$02DC      ; Vertical angle
         STY $C7         ; Set for field
         JSR C3DDD2      ; Make matrix tbl
         LDA #$10        ; Y: 16
         STA $3B         ; Set BG2 Y-Pos
         LDX #$9F2C      ; Src: C2/9F2C
         LDA #$1B        ; Sprites: 27
         JSR C3D9F1      ; Build text OAM
         LDY #C3D933     ; C3/D933
         JSR qanim2      ; Queue credits
         LDY #C3CF9A     ; C3/CF9A
         JSR qanim0      ; Queue BG scroll
         PEA $8D3E       ; Anim ptr LBs
         PEA $1444       ; Pos: 68, 20
         JSR C3CFDC      ; Queue SW rotor
         PEA $8D91       ; Anim ptr LBs
         PEA $14AC       ; Pos: 172, 20
         JSR C3CFDC      ; Queue SE rotor
         PEA $8D3E       ; Anim ptr LBs
         PEA $E860       ; Pos: 96, 232
         JSR C3CFDC      ; Queue NW rotor
         PEA $8D91       ; Anim ptr LBs
         PEA $E890       ; Pos: 144, 232
         JSR C3CFDC      ; Queue NE rotor
         PEA $8C60       ; Anim table ptr
         PEA $F848       ; Pos: 72, 248
         JSR C3CFDC      ; Queue airship
         LDY #$08AC      ; Frames: 2220
;        STY $20         ; Set fade delay
;        STZ $26         ; Next: Fade-out
         JMP C3C66_      ; Queue fade-in

; Scroll field and sky for rearview airship scene
C3CF9A:  LDA $23         ; NMI count
         AND #$7F        ; 0 or 128?
         BNE C3CFA2      ; Branch if not
         INC $3B         ; Shift sky up
C3CFA2:  LSR A           ; Odd NMI count?
         BCS C3CFAE      ; Exit if so
         REP #$20        ; 16-bit A
         DEC $C1         ; Center Y -1
         DEC $37         ; Move field down
C3CFAE:  SEC             ; Set to requeue
         RTS

; Queue frontview or rearview airship or rotor
C3CFDC:  LDY #C3CFF8     ; C3/CFF8
         JSR qanim2      ; Queue OAM fn
         JSR C3D239      ; Ptr HB: C2
         PLY             ; Return address
         PLA             ; X position
         JSR C30940      ; Set sprite's
         PLA             ; Y position
         STA $7E354A,X   ; Set sprite's
         REP #$20        ; 16-bit A
         PLA             ; Anim table ptr
         PHY             ; Repush address
         JMP setaniY     ; Set ptr LBs

; Animate frontview or rearview airship or rotor
C3CFF8:  BNE C3D00B      ; If mode 1

; Mode 0: Initialize timers
C3D000:  STZ $3349,X     ; Anim timer: 0
         JSR C3120_      ; Set pose timer

; Mode 1: Sustain sprite
C3D00B:  JSR C3CBFF      ; Set altitute
C3D010:  INC $3349,X     ; Anim timer +1
         JMP C30860      ; Set OAM; requeue

; Load colors for blue sky and non-sprite airships
C3D018:  LDY #$3049      ; To BG pal 0
         JSR C3D01_      ; Load fill color
         LDY #$3069      ; To BG pal 1
C3D01_:  LDX #$959C      ; Source LBs
         JMP pal_c2      ; Load the rest

; 20: Start final airship scene
C3D02F:  JSR C3C59F      ; Color math: BG1
         LDA #$80        ; Map wraps: No
         STA $211A       ; Set M7 settings
         JSR C3D6B1      ; Unpack map, GFX
         JSR C3D66B      ; Pal numbers loc
         JSR C3D552      ; Convert M7 GFX A
         JSR C3D625      ; Insert tilemap
         JSR C3D634      ; Upload M7 map A
         JSR C3D562      ; Convert M7 GFX B
;        JSR C3D625      ; Insert tilemap B
         JSR C3D64C      ; Upload M7 map B
         JSL $D4CE55     ; HDMA: MS desig
         JSR C3D018      ; Load BG colors
;        LDY #$0001      ; Zoom-out: 1
         INC $B7         ; Set matrix A
         INC $BD         ; Set matrix D
         LDY #$0008      ; X: 8
         STY $BF         ; Set center X
         LDA #$18        ; Y: 24
         STA $C1         ; Set center Y
         LDY #$FFD8      ; X: -40
         STY $35         ; Set BG1 X-Pos
         LDY #$FFD0      ; Y: -48
         STY $37         ; Set BG1 Y-Pos
         LDY #C3D122     ; C3/D122
         JSR qanim0      ; Queue zoom-out
         LDY #C3D0A8     ; C3/D0A8
         JSR qanim0      ; Queue rotor fn
         LDY #$0096      ; Frames: 150
;        STY $20         ; Set fade delay
;        INC $26         ; Next: C3/D096
         JMP C3C79_      ; Queue fade-in

; 21: Fade final airship
C3D096:  LDY $20         ; Delay expired?
         BNE C3D143      ; Exit if not
;        STZ $26         ; Next: Fade-out
         JSR C3C7E_      ; Set fade timer
         JSR C3E16B      ; Fade BG pal 1+2
C3E241:  LDY #$3049      ; 7E/3049
         JMP black4      ; Fade BG pal 0

; Animate final airship's rotors
C3D0A8:  REP #$20        ; 16-bit A
         LDA $23         ; NMI count
         LSR A           ; Odd value?
         LDA #$4F51      ; Tiles: 79, 81
         BCS C3D0BF      ; If odd count
         LDA #$5A59      ; Tiles: 90, 89
C3D0BF:  STA $9849       ; Set rotor tiles
         SEP #$21        ; 8-bit A; requeue

; Fork: Set to refresh rotors
C3D0D0:  LDY #$0283      ; L-Rotor adr
         STY $14         ; Set VRAM ptr1
         LDY #$9849      ; 7E/9849
         STY $16         ; Set src1 LBs
         LDA #$7E        ; Bank: 7E
         STA $18         ; Set src1 HB
         LDY #$0001      ; Bytes: 1
         STY $12         ; Set src1 size
         PHY             ; Set src2 size
         LDY #$0206      ; R-Rotor adr
         LDX #$984A      ; Src: 7E/984A
         JMP C3A60Y      ; Set R-Rotor data

; Animate sideview airship's rotors
C3D0F2:  LDA $23         ; NMI count
         LSR A           ; Odd value?
         LDA #$58        ; Tile: 88
         BCS C3D105      ; If odd count
         LDA #$34        ; Tile: 52
C3D105:  STA $9849       ; Set rotors'
         LDY #$0892      ; VRAM: $0892
         LDX #$9849      ; Src: 7E/9849
         PEA $0001       ; Bytes: 1
         SEC             ; Set to requeue
         JMP C30F4Y      ; Set DMA vars

; Process queued zoom-out for final airship scene
C3D122:  LDA $23         ; NMI count
         AND #$07        ; Now 0-7
         REP #$20        ; 16-bit A
         BNE C3D137      ; Branch if > 0
         DEC $C1         ; M7 center Y -1
         INC $BF         ; M7 center X +1
C3D137:  JSR C3D13_      ; Matrix A/D +1
C3D13_:  INC $B7         ; Matrix A +1
         INC $BD         ; Matrix D +1
         SEC             ; Set to requeue
C3D143:  RTS

; Load BG1 palettes for credits scenes
C3D173:  LDY #$3089      ; To BG pal 2
         LDX #$95DC      ; Src LBs
         JSR pal_c2      ; Do sea
         LDY #$30A9      ; To BG pal 3
         LDX #$95FC      ; Src LBs
         JSR pal_c2      ; Do clouds
         LDY #$30C9      ; To BG pal 4
         LDX #$961C      ; Src LBs
         JSR pal_c2      ; Do trees
         LDY #$3109      ; To BG pal 6
         LDX #$963C      ; Src LBs
         JSR pal_c2      ; Do lake
         LDY #$3129      ; To BG pal 7
         LDX #$965C      ; Src LBs
         JMP pal_c2      ; Do grass

; Scroll actor's pseudonym down
C3D1B6:  LDY $CF         ; Scroll timer
         CPY $64         ; At endpoint?
         CLC             ; Preset to dequeue
         BEQ C3D1C8      ; Dequeue if so
         LDA $23         ; NMI count
         AND #$03        ; Now 0-3
         BNE C3D1C6      ; Exit if 1-3
         REP #$20        ; 16-bit A
         DEC $3F         ; BG3 camera up
C3D1C6:  SEC             ; Set to requeue
C3D1C8:  RTS

; Process queued field fade-in
C3D1CC:  LDA $3349,X     ; Timer expired?
         BEQ C3D1F7      ; Finalize if so
         INC $44         ; Brightness +1
         JMP C3DF59      ; Timer -1
C3D1F7:  LDA #$0F        ; BRT: 15 and ON
C3D1F9:  STA $44         ; Set brightness
         CLC             ; Set to dequeue
C3D1FC:  RTS

; Process queued field fade-out
C3D1FD:  DEC $44         ; Brightness -1
         SEC             ; Set to requeue
         LDY $20         ; Timer expired?
         BNE C3D1FC      ; Exit if not
         LDA #$80        ; BRT: 0 and OFF
         BRA C3D1F9      ; Set to dequeue

; Queue Mode 7 animation script loading
C3D225:  PHY             ; Save ptr LBs
         LDY #C3D23F     ; C3/D23F
C3D22C:  JSR qanim1      ; Queue anim fn
         PLY             ; Anim ptr LBs
         JSR setaniX     ; Put in queue
C3D239:  LDA #$C2        ; Anim ptr HB
C3D23A:  STA $7E35CA,X   ; Put in queue
         RTS

; Load parameters for Mode 7 animation
C3D23F:  BNE C3D24F      ; If mode 1

; Mode 0: Initialize sequence timer
C3D247:  JSR C3120_      ; Set anim timer

; Mode 1: Load animation flags
C3D24F:  JSR ld36C_      ; Anim flags
         STY $04         ; Set as current
         SEC             ; Set to requeue
         RTS

; Queue Mode 7 animation script decoding
C3D2A0:  LDY #C3D2E5     ; C3/D2E5
         JMP qanim0      ; Queue anim fn

; Decode parameters for Mode 7 animation
C3D2E5:  LDA $05         ; Zoom out?
         BPL C3D2D1      ; Skip if not
         DEC $C6         ; Divisor -1

; Fork: Rotate backward
C3D2D1:  REP #$20        ; 16-bit A
         LDA $04         ; Anim flags
         ASL A           ; Shift bits
         ASL A           ; Tilt down?
         BCC C3D2ED      ; Skip if not
         DEC $C7         ; Matrix mod -1

; Fork: Scroll southward
C3D2ED:  ASL A           ; Shift flags
         ASL A           ; Scroll down?
         BPL C3D2FB      ; Skip if not
         DEC $C1         ; Center Y -1
         DEC $37         ; BG1 Y-Pos -1

; Fork: Scroll northward
C3D2FB:  ASL A           ; Scroll up?
         BPL C3D309      ; Skip if not
         INC $C1         ; Center Y +1
         INC $37         ; BG1 Y-Pos +1

; Fork: Scroll eastward
C3D309:  ASL A           ; Scroll right?
         BPL C3D317      ; Skip if not
         DEC $BF         ; Center X -1
         DEC $35         ; BG1 X-Pos -1

; Fork: Scroll westward
C3D317:  ASL A           ; Scroll left?
         BPL C3D2DD      ; Skip if not
         INC $BF         ; Center X +1
         INC $35         ; BG1 X-Pos +1

; Fork: Zoom in
C3D2DD:  ASL A           ; Inc divisor?
         BPL C3D2C5      ; Skip if not
         SEP #$20        ; 8-bit A
         INC $C6         ; Divisor +1
         REP #$20        ; 16-bit A

; Fork: Rotate forward
C3D2C5:  ASL A           ; Tilt up?
         BPL C3D2B7      ; Skip if not
         INC $C7         ; Matrix mod +1

; Fork: Rotate counterclockwise
C3D2B7:  ASL A           ; Tilt left?
         BPL C3D2A9      ; Skip if not
         DEC $C3         ; Mod index -1

; Fork: Rotate clockwise
C3D2A9:  ASL A           ; Tilt right?
         BPL C3D325      ; Skip if not
         INC $C3         ; Mod index +1

; Fork: Keep center X and Y within boundaries
C3D338:  LDA #$E000      ; Bits to discard
         TRB $BF         ; Cap center X
         TRB $C1         ; Cap center Y

; Fork: Build Mode 7 matrix tables
C3D325:  LDA $04         ; Anim flags
         AND #$F0FF      ; Only scrolling?
         SEP #$20        ; 8-bit A
         BEQ C3D336      ; Exit if so
         JSR C3DDD2      ; Build tables
C3D336:  SEC             ; Set to requeue
         RTS

; BG mode and tile size per scanline
C3D36A:  db $47,$01      ; Mode 1, 8x8 tiles
         db $01,$07      ; Mode 7
         db $00          ; End

; Load cloudy sky visuals and set up gradient
C3D36F:  JSR C3CA71      ; Load sky visuals
         JSR C3D38C      ; Queue gradient
         LDY #$3001      ; 2Rx1B to PPU
         STY $4330       ; To $2130, $2131
         LDY #C3D3A7     ; C3/D3A7
         STY $4332       ; Set src LBs
         LDA #$C3        ; Bank: C3
         STA $4334       ; Set src HB
         LDA #$08        ; Channel: 3
         BRA C3D38_      ; Queue HDMA-3

; Queue gradient effect for field under sky
C3D38C:  STZ $4320       ; 1Rx1B to PPU
         LDY #C3D3AE     ; C3/D3AE
         LDA #$32        ; $2132
dma2:    STA $4321       ; To fixed color
         STY $4322       ; Set src LBs
         LDA #$C3        ; Bank: C3
         STA $4324       ; Set src HB
dma2on:  LDA #$04        ; Channel: 2
C3D38_:  TSB $43         ; Queue HDMA-2
         RTS

; Color addition and math per scanline
C3D3A7:  db $47,$80,$41  ; BG1, inside window, half mode
C3D3AA:  db $01,$82,$01  ; BG1, inside window, add subscreen
C3D3AD:  db $00          ; End

; Gradient table for the above
C3D3AE:  db $47,$E0      ; RGB: 0
C3D3B0:  db $01,$ED      ; RGB: 13
C3D3B2:  db $01,$EB      ; RGB: 11
C3D3B4:  db $01,$EA      ; RGB: 10
C3D3B6:  db $02,$E9      ; RGB: 9
C3D3B8:  db $03,$E8      ; RGB: 8
C3D3BA:  db $04,$E7      ; RGB: 7
C3D3BC:  db $05,$E6      ; RGB: 6
C3D3BE:  db $06,$E5      ; RGB: 5
C3D3C0:  db $07,$E4      ; RGB: 4
C3D3C2:  db $08,$E3      ; RGB: 3
C3D3C4:  db $0C,$E2      ; RGB: 2
C3D3C6:  db $0F,$E1      ; RGB: 1
C3D3C8:  db $1E,$E0      ; RGB: 0
C3D3CA:  db $00          ; End

; Unpack and upload font graphics for credits
C3D439:  JSR bigfont     ; Load big font

; Fork: Expand large font graphics to 16x16
C3D497:  JSR db7e_       ; Set DB to 7E
         TAX             ; ROM index: 0
         TAY             ; Dst index: 0
C3D49F:  PHX             ; Save ROM index
         TDC             ; Clear A
         LDA $C29854,X   ; Tile index
         REP #$20        ; 16-bit A
         JSR x16         ; x16
         TAX             ; Index it
         SEP #$20        ; 8-bit A
         LDA #$10        ; Rows: 8
C3D4B2:  PHA             ; Save counter
         LDA $C000,X     ; 8 px, 1 plane
         STA $C400,Y     ; Put in buffer
         TDC             ; Blank plane
         STA $C410,Y     ; To next tile
         INX             ; Src index +1
         INY             ; Dst index +1
         PLA             ; Rows left
         DEC A           ; Rows left -1
         BNE C3D4B2      ; Loop till zero
         LDA #$10        ; Counter: 16
.loop    INY             ; Dst index +1
         DEC A           ; Counter -1
         BNE .loop       ; Loop till 0
         PLX             ; ROM index
         INX             ; ROM index +1
         CPX #$0040      ; Done 64 tiles?
         BNE C3D49F      ; Loop if not
         PLB             ; Restore DB

; Fork: Upload both fonts
         LDX #$00C0      ; Bytes: 3072
;        LDY #$7000      ; VRAM pointer
         JSR C3EE8A      ; Upload font
         LDY #$87C0      ; C4/87C0
         LDA #$C4        ; Bank: C4
         JSR C3D51D      ; Define source
         LDX #$0020      ; Bytes: 512
         LDY #$7E00      ; VRAM pointer
         JSR C3DD86      ; Do small font

; Fork: Upload period and space character for special thanks
C3D46F:  LDY #$8C10      ; C4/8C10
         LDA #$C4        ; Bank: C4
         JSR E7_35C_     ; Set src; Y=0
         TYX             ; Clear X
         INX             ; Bytes: 16
         LDY #$7FA0      ; VRAM pointer
         JSR C3DD86      ; Upload period
         LDY #$0010      ; Bytes: 32
         JMP C3DD7A      ; Create space

; Add 4bpp graphics to Mode 7 map (first 128 tiles)
C3D552:  LDX $00         ; Pal index: 0
         LDY #$9800      ; Source LBs
         BRA C3D56_      ; Branch below

; Add 4bpp graphics to Mode 7 map (last 128 tiles)
C3D562:  LDX #$0080      ; Pal index: 128
         LDY #$A800      ; Source LBs
C3D56_:  STX $E4         ; Set pal index
         LDA #$7F        ; Src HB: 7F
         JSR E7_35C_     ; Set src; Y=0
         TYX             ; Dst index: 0
C3D75A:  LDY #$0008      ; Rows left: 8

; Fork: Load 8-pixel row
C3D75E:  PHY             ; Save counter
         REP #$20        ; 16-bit A
         LDY #$0010      ; Src index: 16
         LDA [$E7]       ; Planes A & B
         STA $F1         ; Memorize them
         LDA [$E7],Y     ; Planes C & D
         STA $EF         ; Memorize them
         SEP #$20        ; 8-bit A

; Fork: Convert each pixel in row
C3D76E:  LDA #$08        ; Pixels: 8
C3D771:  PHA             ; Save counter
         TDC             ; Clear A
         ASL $F0         ; Get plane D
         ROL A           ; Roll into A
         ASL $EF         ; Get plane C
         ROL A           ; Roll into A
         ASL $F2         ; Get plane B
         ROL A           ; Roll into A
         ASL $F1         ; Get plane A
         ROL A           ; Roll into A
;        BEQ C3D792      ; If blank pixel
         STA $E0         ; Save 0-15 color
         LDY $E4         ; Palette index
         LDA [$91],Y     ; Palette number
         JSR x16         ; Palette x16
         ORA $E0         ; Add 0-15 color
C3D792:  STA $7FB801,X   ; Set map pixel
         INX             ; Dst index +1
         INX             ; Dst index +1
         PLA             ; Pixels left
         DEC A           ; One less left
         BNE C3D771      ; Loop till last

; Fork: Do tile's next row
C3D79A:  REP #$21        ; 16-bit A; C-
         INC $E7         ; Source +1
         INC $E7         ; Source +1
         PLY             ; Rows left
         DEY             ; One less left
         BNE C3D75E      ; Loop till last

; Fork: Do next tile
C3D7A4:  INC $E4         ; Pal index +1
         LDA $E7         ; Source
         ADC #$0010      ; Skip done data
         STA $E7         ; Save changes
         CPX #$4000      ; Done 128 tiles?
         BNE C3D75A      ; Loop if not
         SEP #$20        ; 8-bit A
         RTS

; Upload upper half of Mode 7 map
C3D634:  PEI ($00)       ; VRAM: $0000
         BRA C3D64_      ; Skip a line

; Upload lower half of Mode 7 map
C3D64C:  PEA $2000       ; VRAM pointer
C3D64_:  LDY #$B800      ; 7F/B800
         LDA #$7F        ; Bank: 7F
         JSR C3D51D      ; Define source
         LDX #$4000      ; Bytes: 16384
         PLY             ; VRAM pointer
         JMP C3DDB_      ; Upload map

; Unpack and upload graphics and tilemaps for grassfield scenes
C3D573:  JSR C3D57_      ; Upload font, etc.
C3D57X:  JSR C3D664      ; Pal numbers loc
         JSR C3D552      ; Convert M7 GFX A
         JSR C3D59B      ; Insert tilemap A
         JSR C3D634      ; Upload M7 map A
         JSR C3D562      ; Convert M7 GFX B
;        JSR C3D59B      ; Insert tilemap B
         BRA C3D64C      ; Upload M7 map B

; Unpack and upload graphics and tilemaps for second-last airship scene
C3D57F:  JSR C3D694      ; Unpack map, GFX
         JSR C3D57X      ; Upload M7 map
         RTL

; Unpack and upload graphics and tilemaps for dense clouds
C3D522:  JSR C3D439      ; Upload font GFX
         JSR C3D6B1      ; Unpack map, GFX
         JSR C3D6D6      ; Upload sprites
         JSR C3D66B      ; Pal numbers loc
         JSR C3D552      ; Convert M7 GFX A
         JSR C3D543      ; Insert tilemap A
         JSR C3D634      ; Upload M7 map A
         JSR C3D562      ; Convert M7 GFX B
;        JSR C3D543      ; Insert tilemap B
         BRA C3D64C      ; Upload M7 map B

; Unpack and upload visuals for credits scenes
C3D57_:  JSR C3D439      ; Upload font GFX
         JSR C3D694      ; Unpack map, GFX

; Fork: Upload non-font sprite graphics
C3D6D6:  JSR C3D518      ; Src: 7E/C000
         LDX #$17BA      ; Bytes: 6074
;        LDY #$6000      ; VRAM pointer
         JMP C3F050      ; Upload data

; Assemble half of tilemap for sideview airship scene
C3D42A:  LDX #$FB09      ; Table loc LBs
;        LDA #$CF        ; Table loc HB
         JSR C3D678      ; Set pointer
         BRA C3D546      ; Build map

; Assemble half of tilemap for dense clouds
C3D543:  JSR C3D686      ; Set ptrs loc
C3D546:  LDX #$FC1A      ; Tiles loc
         BRA C3D59_      ; Build map

; Assemble half of grassfield tilemap
C3D59B:  JSR C3D675      ; Set ptrs loc
         LDX #$DD3A      ; Tiles loc
C3D59_:  LDA #$7E        ; Bank: 7E
         BRA C3D62Y      ; Build map

; Assemble half of tilemap for sea with boat
C3D5CB:  LDX #$FAC9      ; Table loc LBs
;        LDA #$CF        ; Table loc HB
         JSR C3D678      ; Set pointer
         BRA C3D5FE      ; Build map

; Assemble half of tilemap for sea without boat
C3D5FB:  JSR C3D686      ; Set ptrs loc
C3D5FE:  LDX #$2CFA      ; Tiles loc
         BRA C3D62X      ; Build map

; Assemble half of tilemap for final airship scene
C3D625:  JSR C3D675      ; Set ptrs loc
         LDX #$041A      ; Tiles loc
C3D62X:  LDA #$7F        ; Bank: 7F
C3D62Y:  STX $F1         ; Set src LBs
         STA $F3         ; Set src HB

; Fork: Add a 128x64 tilemap to Mode 7 map
C3D706:  LDY $00         ; Index: 0
C3D708:  REP #$20        ; 16-bit A
         LDA [$F7],Y     ; Src index
         TAX             ; Save in X
         INY             ; Index +1
         INY             ; Index +1
         LDA [$F7],Y     ; Dst index
         STA $E7         ; Memorize it
         INY             ; Index +1
         INY             ; Index +1
         SEP #$20        ; 8-bit A
         PHY             ; Save index
         TXY             ; Src index

; Fork: Add current 32x32 tilemap
C3D723:  LDA #$20        ; Rows: 32
C3D72C:  PHA             ; Save counter
         LDX $E7         ; Dst index
         LDA #$20        ; Columns: 32
C3D732:  PHA             ; Save counter
         LDA [$F1],Y     ; Tile number
         STA $7FB800,X   ; Add to M7 map
         INY             ; Src index +1
         INX             ; Dst index +1
         INX             ; Dst index +1
         PLA             ; Columns left
         DEC A           ; One less left
         BNE C3D732      ; Loop till last
         INC $E8         ; Dst: +128 tiles
         PLA             ; Rows left
         DEC A           ; One less left
         BNE C3D72C      ; Loop till last
         PLY             ; Restore index
         CPY #$0020      ; Done 8 maps?
         BNE C3D708      ; Loop if not
         RTS

; Set palette index table location for plains or sea
C3D664:  LDX #$F9E1      ; Ptrs loc LBs
         BRA C3D66E      ; Skip a line

; Set palette index table location for sky scenes
C3D66B:  LDX #$F969      ; CF/F969
C3D66E:  LDA #$CF        ; Bank: CF
C3D670:  STX $91         ; Set loc LBs
         STA $93         ; Set loc HB
         RTS

; Set tilemap pointer table location for skimming airship and cloud scenes
C3D686:  LDX #$FAA9      ; Table loc LBs
         BRA C3D678      ; Skip a line

; Set tilemap pointer table location for final scenes
C3D675:  LDX #$FAE9      ; CF/FAE9
C3D678:  LDA #$CF        ; Bank: CF
C3D67A:  STA $F9         ; Set loc HB
         STX $F7         ; Set loc LBs
         RTS

; Unpack credits scene GFX and maps, move ocean and grassfield GFX
C3D694:  JSR C3D6CE      ; Unpack data
         LDY #$141A      ; 7F/141A
         LDA #$7F        ; Bank: 7F
         JSR C3D51D      ; Set source
         LDY #$18E0      ; Bytes: 6368
         BRA C3D6B_      ; Branch below

; Unpack credits scene GFX and tilemaps, move cloud and BG airship GFX
C3D6B1:  JSR C3D6CE      ; Unpack data
         LDY #$ED3A      ; 7E/ED3A
         JSR C3D51B      ; Set source
         LDY #$0EDF      ; Bytes: 3808
C3D6B_:  LDX #$9800      ; 7F/9800
         LDA #$7F        ; Bank: 7F
         STA $ED         ; Set dst HB
         STX $EB         ; Set dst LBs
         JMP C3EF1C      ; Move data

; Decompress large ending font
bigfont: LDX #$BA00      ; Source LBs
bigfon_: LDA #$C4        ; Source HB
         BRA C3DD61      ; Unpack font

; Unpack non-font graphics for credits scenes
C3D6CE:  LDX #$568F      ; Source LBs
C3D6C_:  LDA #$D9        ; Source HB
C3DD61:  LDY #$C000      ; 7E/C000
C3DD6_:  STY $F6         ; Set dst LBs
         STX $F3         ; Set src LBs
         STA $F5         ; Set src HB
         LDA #$7E        ; Bank: 7E
         STA $F8         ; Set dst HB
         JSL $C2FF6D     ; Unpack data

; Fork: Set source for uploading decompressed data
C3D518:  LDY #$C000      ; 7E/C000
C3D51B:  SEP #$20        ; 8-bit A
         LDA #$7E        ; Bank: 7E
C3D51D:  STA $E9         ; Set src HB
         STY $E7         ; Set src LBs
         RTS

; Handle credits timing for sideview airship scene
C3D7B9:  LDX #$9894      ; Source LBs
         LDY $CF         ; Scene timer
         CPY #$003C      ; 60?
         BNE C3D816      ; Exit if not
         BRA credx3a     ; Init producer

; Handle credits timing for aerial-view airship scene
C3D7CB:  LDX #$98A0      ; Source LBs
         LDY $CF         ; Scene timer
         CPY #$000A      ; 10?
         BEQ credx5a     ; Branch if so

; Fork: Main programmer
C3D7DC:  LDX #$98B4      ; Source LBs
         CPY #$01AE      ; Timer = 430?
         BEQ credx6a     ; Branch if so

; Fork: Graphic director (page 1)
C3D7E6:  LDX #$98CC      ; Source LBs
         CPY #$0352      ; Timer = 850?
         BEQ credx6a     ; Branch if so

; Fork: Graphic director (page 2)
C3D7F0:  LDX #$98E4      ; Source LBs
         CPY #$04F6      ; Timer = 1270?
         BEQ credx6a     ; Branch if so

; Fork: Music
C3D7FA:  LDX #$98FC      ; Source LBs
         CPY #$069A      ; Timer = 1690?
credx3a: BEQ credx3      ; Branch if so

; Fork: Image design
C3D804:  LDX #$9908      ; Source LBs
         CPY #$083E      ; Timer = 2110?
         BEQ credx4      ; Branch if so

; Fork: Battle planner
C3D80E:  LDX #$9918      ; Source LBs
         CPY #$09E2      ; Timer = 2530?
credx6a: BEQ credx6      ; Branch if so
C3D816:  SEC             ; Set to requeue
         RTS

; Handle credits timing for boat scene
C3D819:  LDX #$9930      ; Source LBs
         LDY $CF         ; Scene timer
         CPY #$000A      ; 10?
         BEQ credx8      ; Branch if so

; Fork: Event planner
C3D82A:  LDX #$9950      ; Source LBs
         CPY #$01AE      ; Timer = 430?
         BEQ credx6      ; Branch if so

; Fork: Effect programmer
C3D834:  LDX #$9968      ; Source LBs
         CPY #$0352      ; Timer = 850?
         BEQ credx6      ; Branch if so

; Fork: Battle programmer
C3D83E:  LDX #$9980      ; Source LBs
         CPY #$04F6      ; Timer = 1270?
         BEQ credx4      ; Branch if so
C3D846:  SEC             ; Set to requeue
         RTS

; Handle credits timing for skimming airship scene
C3D849:  LDX #$9990      ; Source LBs
         LDY $CF         ; Scene timer
         CPY #$000A      ; 10?
         BEQ credx4      ; Branch if so

; Fork: Effect graphic designer
C3D85A:  LDX #$99A0      ; Source LBs
         CPY #$01AE      ; Timer = 430?
credx5a: BEQ credx5      ; Branch if so

; Fork: Field graphic designer (page 1)
C3D864:  LDX #$99B4      ; Source LBs
         CPY #$0352      ; Timer = 850?
         BEQ credx9      ; Branch if so

; Fork: Field graphic designer (page 2)
C3D86E:  LDX #$99D8      ; Source LBs
         CPY #$04F6      ; Timer = 1270?
         BEQ credx9      ; Branch if so

; Fork: Field graphic designer (page 3)
C3D878:  LDX #$99FC      ; Source LBs
         CPY #$069A      ; Timer = 1690?
         BEQ credx9      ; Branch if so

; Fork: Monster graphic designer
C3D882:  LDX #$9A20      ; Source LBs
         CPY #$083E      ; Timer = 2110?
         BEQ credx5      ; Branch if so

; Fork: Object graphic designer
C3D88C:  CPY #$09E2      ; 2530?
         BNE C3D846      ; Exit if not
         LDX #$9A34      ; Source LBs
         BRA credx5      ; Init sprites

; Initialize credits (3 sprites)
credx3:  LDA #$03        ; Sprites: 3
         BRA credini     ; Init sprites

; Initialize credits (4 sprites)
credx4:  LDA #$04        ; Sprites: 4
         BRA credini     ; Init sprites

; Initialize credits (5 sprites)
credx5:  LDA #$05        ; Sprites: 5
         BRA credini     ; Init sprites

; Initialize credits (6 sprites)
credx6:  LDA #$06        ; Sprites: 6
         BRA credini     ; Init sprites

; Initialize credits (8 sprites)
credx8:  LDA #$08        ; Sprites: 8
         BRA credini     ; Init sprites

; Initialize credits (9 sprites)
credx9:  LDA #$09        ; Sprites: 9
credini: JMP C3DBA5      ; Init sprites

; Handle credits timing for frontview airship scene
C3D897:  LDX #$9A48      ; Source LBs
         LDY $CF         ; Scene timer
         CPY #$000A      ; 10?
         BEQ credx4      ; Branch if so

; Fork: Sound effect (page 1)
C3D8A8:  LDX #$9A58      ; Source LBs
         CPY #$01AE      ; Timer = 430?
         BEQ credx6      ; Branch if so

; Fork: Sound effect (page 2)
C3D8B2:  LDX #$9A70      ; Source LBs
         CPY #$0352      ; Timer = 850?
         BEQ credx6      ; Branch if so

; Fork: Remake planner, translator
C3D8BC:  LDX #$9A88      ; Source LBs
         CPY #$04F6      ; Timer = 1270?
         BEQ credx9      ; Branch if so

; Fork: System engineer (page 1)
C3D8C6:  LDX #$9AAC      ; Source LBs
         CPY #$069A      ; Timer = 1690?
         BEQ credx6      ; Branch if so

; Fork: System engineer (page 2)
C3D8D0:  LDX #$9AC4      ; Source LBs
         CPY #$083E      ; Timer = 2110?
         BEQ credx6      ; Branch if so

; Fork: Executive producer
C3D8DA:  CPY #$09E2      ; Timer = 2530?
         BNE C3D96B      ; Exit if not
         LDX #$9ADC      ; Source LBs
credx6b: BRA credx6      ; Init sprites

; Handle credits timing for aerial-view plains scene
C3D8E5:  LDX #$9AF4      ; Source LBs
         LDY $CF         ; Scene timer
         CPY #$00F0      ; 240?
         BEQ credx10     ; Branch if so

; Fork: Special thanks (page 1)
C3D8F6:  LDX #$9B1C      ; Source LBs
         CPY #$02D0      ; Timer = 720?
         BEQ credx10     ; Branch if so

; Fork: Special thanks (page 2)
C3D900:  LDX #$9B44      ; Source LBs
         CPY #$04B0      ; Time = 1200?
         BEQ credx9      ; Branch if so

; Fork: Special thanks (page 3)
C3D90A:  LDX #$9B68      ; Source LBs
         CPY #$0690      ; Time = 1680?
         BEQ credx8      ; Branch if so

; Fork: Special thanks (page 4)
C3D914:  LDX #$9B88      ; Source LBs
         CPY #$0870      ; Timer = 2160?
         BEQ credx6      ; Branch if so

; Fork: Special thanks (page 5)
C3D91E:  LDX #$9BA0      ; Source LBs
         CPY #$0A50      ; Timer = 2640?
credx5b: BEQ credx5      ; Branch if so

; Fork: Special thanks (page 6)
C3D928:  LDX #$9BB4      ; Source LBs
         CPY #$0C30      ; Timer = 3120?
         BEQ credx9      ; Branch if so
C3D930:  SEC             ; Set to requeue
         RTS

; Handle credits timing for rearview airship scene
C3D933:  LDX #$9BD8      ; Source LBs
         LDY $CF         ; Scene timer
         CPY #$003C      ; 60?
         BEQ credx6b     ; Branch if so

; Fork: Special thanks (page 8)
C3D944:  LDX #$9BF0      ; Source LBs
         CPY #$01E0      ; Timer = 480?
         BEQ credx6b     ; Branch if so

; Fork: Special thanks (page 9)
C3D94E:  LDX #$9C08      ; Source LBs
         CPY #$0384      ; Timer = 900?
         BEQ credx5b     ; Branch if so

; Fork: Special thanks (page 10)
C3D958:  LDX #$9C1C      ; Source LBs
         CPY #$0528      ; Timer = 1320?
         BEQ credx5b     ; Branch if so

; Fork: Special thanks (page 11)
C3D962:  LDX #$9C30      ; Source LBs
         CPY #$06CC      ; Timer = 1740?
         BEQ credx5b     ; Branch if so
C3D96B:  SEC             ; Set to requeue
         RTS

; Initialize credits (10 sprites)
credx10: LDA #$0A        ; Sprites: 10

; Initialize sprites for current credits strings
C3DBA5:  PHA             ; Save counter
         REP #$21        ; 16-bit A; C-
         LDA $C20000,X   ; Anim table ptr
         TAY             ; Save in Y
         LDA $C20002,X   ; Position
         ADC #$2000      ; Go 32 px down
         PHX             ; Save index
         JSR C3DC14      ; Init sprite
         PLX             ; Table index
         INX             ; Index +1
         INX             ; Index +1
         INX             ; Index +1
         INX             ; Index +1
         PLA             ; Sprites left
         DEC A           ; One less left
         BNE C3DBA5      ; Loop till last
         SEC             ; Set to requeue
         RTS

; Build OAM table for scene's small or large credits strings
C3D9F1:  LDY #$0B00      ; ADR+, pal 5
         BRA C3D9FX      ; Skip a line

; Build OAM table for scene's large credits strings; set source
C3D9FA:  LDY #$0900      ; ADR+, pal 4
C3D9FX:  STY $F1         ; Save attribs
C3D9FY:  PHA             ; Save counter
         PHX             ; Save index
         CPX #$9DC0      ; Small credits?
         PHP             ; Save answer
         LDA #$C2        ; Bank: C2
         STA $ED         ; Set src HB
         REP #$20        ; 16-bit A
         LDA $C20002,X   ; Dest LBs
         TAY             ; Save in Y
         LDA $C20000,X   ; Text ptr
         STA $EB         ; Set src LBs
         JSR C3D51B      ; Set dest

; Fork: Set OAM table pointer for string, etc.
C3DD31:  REP #$20        ; 16-bit A
         INY             ; Dst LBs +1
         INY             ; Dst LBs +1
         TYA             ; Put in A
         INC A           ; Add one
         STA [$E7]       ; Set OAM ptr
         STY $E7         ; Save dst LBs
         SEP #$20        ; 8-bit A
         LDA #$FE        ; Time: Infinite
         STA [$E7]       ; Set pose timer
         INC $E7         ; Dst LBs +1

; Fork: Compute string size
         LDY $00         ; Letter: 1st
         DEY             ; Letter index -1
C3DD55:  INY             ; Letter index +1
         LDA [$EB],Y     ; At terminator?
         BNE C3DD55      ; Loop if not
         TYA             ; String size
         LDY $00         ; Index: 0
         STA [$E7],Y     ; Set size
         INY             ; Index +1

; Fork: Build OAM table for string
C3DCBC:  TDC             ; Rel X: 0
         PLP             ; Small credits?
         BCS .small      ; Branch if so
         ORA #$80        ; Add 16x16 flag
.small   STA $E0         ; Set X and flag
.loop    SEP #$21        ; 8-bit A; C+
         LDA [$EB]       ; At terminator?
         BEQ C3DD5_      ; Branch if so
         PHA             ; Save letter
         LDA $E0         ; Relative X
         STA [$E7],Y     ; Set letter's
         ADC #$07        ; Go 8 px right
         STA $E0         ; For next letter
         INY             ; Index +1
         TDC             ; Relative Y: 0
         STA [$E7],Y     ; Set letter's
         INY             ; Index +1
;        TDC             ; Clear A
         PLA             ; Letter
         REP #$21        ; 16-bit A; C-
         ADC $F1         ; Add attribs
         STA [$E7],Y     ; Set attribs
         INC $EB         ; Letter +1
         INY             ; Index +1
         INY             ; Index +1
         BRA .loop       ; Do next letter

; Fork: Handle next string
C3DD5_:  PLX             ; Pointer index
         INX             ; Index +1
         INX             ; Index +1
         INX             ; Index +1
         INX             ; Index +1
         PLA             ; Entries left
         DEC A           ; One less left
         BNE C3D9FY      ; Loop till last
         RTS

; Initialize sprite for current credit string
C3DC14:  PHA             ; Save position
         PHY             ; Save anim ptr
         LDY #C3DC44     ; C3/DC44
         JSR qanim0      ; Queue anim fn
         STZ $AF         ; Request fade-in
         REP #$20        ; 16-bit A
         LDA #$01A4      ; Frames: 420
         JSR C3DFFB      ; Set hide delay
         LDA #$7E        ; Anim ptr HB
         JMP C3EA0W      ; Set object data

; Animate credit string
C3DC44:  BNE C3DC84      ; If mode 1

; Mode 0: Initialize sprite
C3DC4C:  REP #$20        ; 16-bit A
         LDA #$FF80      ; V-Spd: -0.5 px
         STA $3549,X     ; Set sprite's
         STZ $34C9,X     ; H-Speed: 0
         JSR C3120_      ; Set pose timer
         LDA $AF         ; Handled fade-in?
         BNE C3DC84      ; Skip it if so
         LDY #$31C9      ; To OBJ pal 4
         LDA $B4         ; Text on clouds?
         BNE C3DC7C      ; Dark blue if so

; Fork: Set to fade to white and blue
C3DC77:  JSR C3E238      ; Do big font
         LDX #$974C      ; C2/974C
         BRA C3DC7_      ; Do small font

; Fork: Set to fade to dark blue
C3DC7C:  LDX #$9744      ; C2/9744
         JSR c2slw4      ; Do big font
         LDX #$973C      ; C2/973C
C3DC7_:  LDY #$31E9      ; To OBJ pal 5
         JSR c2slw4      ; Do small font
         INC $AF         ; Allow fade-out
         LDX $2D         ; Queue index

; Mode 1: Sustain sprite
C3DC84:  CLC             ; Preset to dequeue
         LDY $3349,X     ; Mode expired?
         BEQ C3DD85      ; Delete if so
         CPY #$0164      ; 356?
         BNE C3DC96      ; Scroll if not
         STZ $3549,X     ; V-Speed LB: 0
         STZ $354A,X     ; V-Speed HB: 0
C3DC96:  CPY #$0080      ; 128?
         BNE C3DC9B      ; Branch if not

; Fork: Set to fade all text
C3DCAB:  LDA $AF         ; Already set?
         BEQ C3DC9B      ; Skip if so
         STZ $AF         ; Mark as handled
         JSR C3E29E      ; Do big font
         LDY #$31E9      ; To OBJ pal 5
         JSR black4      ; Do small font
C3DC9B:  JSR C3DEC0      ; Scroll & OAM
         JMP C3CFE5      ; Timer -1; requeue

; Clear VRAM
C3DD73:  REP #$20        ; 16-bit A
         STZ $2116       ; VRAM: $0000
         LDY #$8000      ; Bytes: 65536
C3DD7A:  REP #$20        ; 16-bit A
.loop    STZ $2118       ; Zero VRAM word
         DEY             ; One less word
         BNE .loop       ; Loop till last
C3DD83:  SEP #$20        ; 8-bit A
C3DD85:  RTS             ; Y must = 0

; Upload data manually, adjust attributes if tilemap
C3DDB_:  STZ $EE         ; Attrib HB: 0
C3DDB9:  STZ $ED         ; Attrib LB: 0
         STY $2116       ; Set VRAM ptr
C3DDC1:  REP #$21        ; 16-bit A; C-
         LDA [$E7]       ; Source data
         ADC $ED         ; Add attribs
         STA $2118       ; Save in VRAM
         INC $E7         ; Source +1
         INC $E7         ; Source +1
         DEX             ; Counter -1
         DEX             ; Counter -1
         BNE C3DDC1      ; Loop till 0
         BRA C3DD83      ; 8-bit A; exit

; Build Mode 7 matrix tables for HDMA
C3DDDX:  STX $C3         ; Set mod index
         STY $C7         ; Set angle
C3DDDY:  STA $C6         ; Set divisor
C3DDD2:  PHB             ; Save DB
         TDC             ; Zero A
         PHA             ; Put on stack
         PLB             ; Set DB to 0

; Fork: Define scale dividend
C3DDD9:  LDA $C3         ; Dividend index
         JSR C3DE76      ; Signed dividend
         STA $EB         ; Memorize it
         BPL C3DDEA      ; If positive
         JSR invert      ; Invert value
C3DDEA:  LSR A           ; Halve it
         STA $CB         ; Save dividend

; Fork: Define shear dividend
C3DDEF:  LDA $C3         ; Dividend index
         JSR C3DE7A      ; Signed dividend
         STA $ED         ; Memorize it
         BPL C3DE00      ; If positive
         JSR invert      ; Invert value
C3DE00:  LSR A           ; Halve it
         STA $C9         ; Save dividend

; Fork: Define matrices A and D for two scanlines
C3DE05:  LDY #$01BE      ; Scanline: Last
         LDA $C5         ; Base divisor
         STA $E7         ; Memorize it
C3DE0C:  LDX $CB         ; Scale dividend
         JSR C3DDD_      ; Divide by $E8
         LDX $EB         ; Positive mod?
         BPL C3DE2F      ; Branch if so
         JSR invert      ; Invert quotient
C3DE2F:  STA $0602,Y     ; Set line 1
         STA $0604,Y     ; Set line 2

; Fork: Define matrices B and C for two scanlines
C3DE35:  LDX $C9         ; Shear dividend
         JSR C3DDD_      ; Divide by $E8
         LDX $ED         ; Positive mod?
         BPL C3DE5C      ; Branch if so
         JSR invert      ; Invert quotient
C3DE5C:  STA $07C4,Y     ; Set line B1
         STA $07C6,Y     ; Set line B2
         JSR invert      ; Invert it
         STA $0986,Y     ; Set line C1
         STA $0988,Y     ; Set line C2
         LDA $E7         ; Matrix divisor
         SEC             ; Prepare SBC
         SBC $C7         ; Deduct "angle"
         STA $E7         ; Save changes
         DEY             ; Index -1
         DEY             ; Index -1
         DEY             ; Index -1
         DEY             ; Index -1
         BPL C3DE0C      ; Loop till < 0
         SEP #$20        ; 8-bit A
         PLB             ; Restore DB
C3DE83:  RTS

; Compute matrix value
C3DDD_:  SEP #$20        ; 8-bit A
         LDA $E8         ; Map factor HB
         JSR divide      ; Dividend / factor
         REP #$20        ; 16-bit A
         TXA             ; Quotient
         RTS

; Load animation modifier
C3DE76:  REP #$21        ; 16-bit A; C-
         ADC #$0040      ; Index +64
C3DE7A:  AND #$00FF      ; Clear HB
         ASL A           ; Double it
         TAX             ; Index it
         LDA $C2FC6D,X   ; Modifier
         LDX $2D         ; Queue index
         TAY             ; Modifier
         RTS

; Animate linearly moving temporary sprite
C3DE84:  BNE C3DE94      ; If mode 1

; Mode 0: Initialize timer
C3DE8C:  JSR C3120_      ; Set pose timer

; Mode 1: Sustain sprite
C3DE94:  CLC             ; Preset to dequeue
         LDY $3349,X     ; Timer expired?
         BEQ C3DE83      ; Delete if so
         JMP C3DC9B      ; Scroll & OAM

; Animate regular permanent sprite
C3DEAB:  BNE C3DEBB      ; If mode 1

; Mode 0: Initialize timer
C3DEB3:  JSR C3120_      ; Set pose timer

; Mode 1: Sustain sprite
C3DEBB:  JMP C3EBD0      ; Scroll & OAM

; Scroll sprite and define OAM
C3DEC0:  LDX $2D         ; Queue index
         REP #$21        ; 16-bit A; C-
         LDA $33C9,X     ; Fractional X
         ADC $34C9,X     ; Add H-Speed
         STA $33C9,X     ; Save changes
         LDA $3449,X     ; Fractional Y
         CLC             ; Prepare ADC
         ADC $3549,X     ; Add V-Speed
         STA $3449,X     ; Save changes
         JMP C31221      ; Define OAM

; Animate true actor name or "And you"
C3DEDD:  TAX             ; Index mode
         LDA $47         ; At 2nd actor?
         BNE C3DEFD      ; Delete if so
         JMP (C3DEEB,X)  ; Handle mode

; Jump table for the above
C3DEEB:  dw C3DEFF       ; Initialize sprite
         dw C3DF13       ; Refresh sprite
         dw C3DF1B       ; Set to scroll up
         dw C3DF13       ; Refresh sprite
         dw C3DF23       ; Stop scrolling
         dw C3DF13       ; Refresh sprite
         dw C3DF2A       ; Set to scroll up
         dw C3DF13       ; Refresh sprite
         dw C3DEFD       ; Delete sprite

; Mode 8: Delete sprite
C3DEFD:  CLC             ; Set to dequeue
         RTS

; Mode 0: Initialize sprite
C3DEFF:  TYX             ; Queue index
         REP #$20        ; 16-bit A
         STZ $3549,X     ; V-Speed: 0
         LDA $85         ; Scroll delay
         STA $3349,X     ; Set mode timer
         JSR C3120_      ; Set pose timer

; Modes 1, 3, 5: Refresh sprite
C3DF13:  JSR C3DF4B      ; Mode timer -1
         BRA C3DEBB      ; Scroll & OAM

; Mode 2: Set to scroll upward 2.5s
C3DF1B:  PEA $0096       ; Frames: 150
;        PEA $FFC0       ; V-Spd: -0.25 px
         BRA C3DF2_      ; Set spd, timer

; Mode 4: Stop scrolling
C3DF23:  PEA $00F0       ; Frames: 240
         PEI ($00)       ; V-Speed: 0
         BRA C3DF32      ; Set spd, timer

; Mode 6: Set to scroll upward 5s
C3DF2A:  PEA $012C       ; Frames: 300
C3DF2_:  PEA $FFC0       ; V-Spd: -0.25 px

; Fork: Change V-Speed and mode timer
C3DF32:  TYX             ; Queue index
         INC $3649,X     ; Mode +1
         REP #$20        ; 16-bit A
         PLA             ; V-Speed
         STA $3549,X     ; Set sprite's
         PLA             ; Mode timer
         STA $3349,X     ; Set sprite's
         BRA C3DF13      ; Scroll & OAM

; Decrease animation timer, advance mode if expired
C3DF4B:  LDX $2D         ; Queue index
         REP #$20        ; 16-bit A
C3DF4F:  LDA $3349,X     ; Timer expired?
         BNE C3DF59      ; Branch if not
         INC $3649,X     ; Mode +1
;        BRA C3DF5C      ; Skip a line
C3DF59:  DEC $3349,X     ; Timer -1
C3DF5C:  SEP #$21        ; 8-bit A; requeue
         RTS

; Draw, center, and set to upload actor's pseudonym
C3DF5F:  LDX #$001E      ; 1st: Guest 2
C3DF61:  PHX             ; Save index
         JSR charadY     ; Actor's address
         TXY             ; Copy to Y
         LDA $00,X       ; Actor in slot
         PLX             ; Restore index
         CMP $28         ; Matches ours?
         BEQ C3DF99      ; Proceed if so
         DEX             ; Actor index -1
         DEX             ; Actor index -1
         BPL C3DF61      ; Loop till < 0

; Fork: Default to "??????" (Gogo, Umaro)
C3DF89:  LDX #$0005      ; 1st: Letter 6
         LDA.B #!ques    ; Char: "?"
C3DF8D:  STA $7E9E89,X   ; Add to string
         DEX             ; Index -1
         BPL C3DF8D      ; Loop till < 0
         BRA C3DFB3      ; Skip alt case

; Fork: Load actor's name
C3DF99:  LDX $00         ; Dst index: 0
C3DF9B:  LDA $0002,Y     ; Letter
         STA $7E9E89,X   ; Add to string
         INY             ; Src index +1
         INX             ; Dst index +1
         CPX #$0006      ; Done 6 chars?
         BNE C3DF9B      ; Loop if not

; Fork: Center BG3 camera on actor's name
C3DFB3:  TDC             ; Clear A
         TAX             ; Letter: 1st
         PHX             ; Text width: 0
C3DFB9:  LDA $7E9E89,X   ; Letter
         CMP #$FF        ; Terminator?
         BEQ C3DFD7      ; Branch if so
         TXY             ; Save index
         SBC #$5F        ; Use field format
         TAX             ; Index it
         PLA             ; Text width
         CLC             ; Prepare ADC
         ADC $C48FC0,X   ; Add letter's
         PHA             ; Save changes
         TYX             ; Letter index
         INX             ; Point to next
         CPX #$0006      ; Done all 6?
         BNE C3DFB9      ; Loop if not
C3DFD7:  REP #$20        ; 16-bit A
         PLA             ; Text width
         LSR A           ; Halve it
         CLC             ; Prepare ADC
         ADC #$FF80      ; 128 - width/2
         STA $3D         ; Set BG3 X-Pos
         SEP #$20        ; 8-bit A

; Fork: Build tilemap
C3DFAC:  LDY #$7DC9      ; Dest: 7E/7DC9
         LDX #$000C      ; Source index
         JSR C3A783      ; Build tilemap

; Fork: Load and set to upload VWF graphics
C3A63B:  LDY #$0240      ; Tiles: 36
         JSR clrvwf2     ; Clear GFX buffer
         LDA #$06        ; Letters: 6
C3A64A:  PHA             ; Save counter
         LDA $7E9E89,X   ; Letter
         JSR C3A8B1      ; Load graphics
         INX             ; Letter index +1
         PLA             ; Letters left
         DEC A           ; One less left
         BNE C3A64A      ; Loop till last
         LDY #$2080      ; VRAM pointer

; Set to upload VWF string graphics (elements, ending pseudonym)
C3A600:  PEA $0120       ; Bytes: 288
C3A60X:  LDX #$A271      ; 7E/A271
C3A60Y:  STX $1D         ; Set src LBs
         STY $1B         ; Set VRAM ptr
         PLX             ; Byte count
         STX $19         ; Set data size
C3A60C:  LDA #$7E        ; Bank: 7E
         STA $1F         ; Set src HB
         RTS

; Temporarily scroll BG1 westward
C3E002:  CLC             ; Preset to dequeue
         LDY $3349,X     ; Timer expired?
         BEQ C3E01D      ; Dequeue if so
         JSR C3CFE5      ; Timer -1
         LDA $23         ; NMI count
         AND #$03        ; Now 0-3
         BNE C3E01C      ; Pause if > 0
         REP #$20        ; 16-bit A
         INC $35         ; BG1 X-Pos +1
C3E01C:  SEC             ; Set to requeue
C3E01D:  RTS

; Create actor's first and last names
C3E022:  JSR ld28x2      ; Actor x2
         STA $E0         ; Memorize it
         ASL A           ; Actor x4
         ADC $E0         ; Actor x6
         ADC #$05        ; Add 5
         TAX             ; Index it
         LDY #$0006      ; Loop counter
.loop    LDA $C28DE4,X   ; Object data
         PHA             ; Memorize it
         DEX             ; Index -1
         DEY             ; Counter -1
         BNE .loop       ; Loop till 0
         PLX             ; Anim tbl ptr
         PLA             ; X position
         JSR .queue      ; Create name A
         PLX             ; Anim tbl ptr
         PLA             ; X position
.queue   XBA             ; X position
         LDA #$D0        ; Y: 208
         XBA             ; Invert both
         TAY             ; Move them to Y
         PHY             ; Save position
         PHX             ; Save anim ptr
         JSR C3E08F      ; Queue anim fn
         JMP C3E8AF      ; Create name B

; Queue true actor name or "And you"
C3E08F:  LDY #C3DEDD     ; C3/DEDD
         JMP qanim1      ; Queue anim fn

; Create text, pan table, fade in non-glint sprites
C3E12X:  JSR C3E28A      ; Fade in sprites
C3E12Y:  LDY #$0138      ; Frames: 312
         JSR C3DFED      ; Scroll table
         BRA C3E123      ; Create text

; Call function below and create text
C3E0B_:  JSR C3E0B9      ; Call fn below
C3E123:  STZ $47         ; Erase name: N
         JSR C3E897      ; Create "as"
         JSR C3DF5F      ; Create alias
;        DEC $45         ; CGRAM: Freeze
         JSR C314AC      ; Upload alias
;        INC $45         ; CGRAM: Refresh
         JSR C3E022      ; Create true name
C3E13D:  LDY #$1800      ; VRAM: $1800
         LDX #$7849      ; Src: 7E/7849
         JSR C30F7_      ; Queue BG3a upload
         JSR C30EE9      ; Queue BG1 upload
         INC $26         ; Scene mode +1
         JSR C33541      ; BRT: 1 + NMI
         JMP C3D1F7      ; BRT: 15 and ON

; Initialize font, BG, and register data for actor scene
C3E0B9:  STA $28         ; Set actor slot
         JSL $D4CE8C     ; Init registers
         JSR C36A3C      ; Clear BG3 A
         JSR C36A28      ; Clear BG2 A
         LDX #$3049      ; To 7E/3049
         LDY #$01A0      ; Palettes: 13
         JSR C3A79_      ; Pals = Black
         JSR C3EE74      ; Load big font
         JSR C3EE90      ; And BG GFX/map
         JSR C3E5EF      ; Set txt fade delay
         JMP C3E192      ; Fade in BGs

; Set to fade BG elements for actor scene (also final airship's)
C3E145:  LDY #$30E9      ; To BG pal 5
         JSR black4      ; Do tabletop
         LDY #$3109      ; To BG pal 6
         JSR black4      ; Do overlay
C3E16B:  LDY #$3069      ; To BG pal 1
         JSR black4      ; Do book, etc.
         LDY #$3089      ; To BG pal 2
         JMP black4      ; Do bones, etc.

; Set to fade in BG elements for actor scene
C3E192:  LDY #$3069      ; To BG pal 1
         LDX #$967C      ; C2/967C
         JSR c2slw2      ; Do book, etc.
         LDY #$3089      ; To BG pal 2
         LDX #$969C      ; C2/969C
         JSR c2slw2      ; Do bones, etc.
         LDY #$30E9      ; To BG pal 5
         LDX #$96DC      ; C2/96DC
         JSR c2slw2      ; Do tabletop
         LDY #$3109      ; To BG pal 6
         LDX #$96FC      ; C2/96FC
         JMP c2slw2      ; Do overlay

; 28: Start Shadow's scene
C3E26E:  LDX #$F477      ; Source LBs
;        LDA #$C4        ; Source HB
         JSR bigfon_     ; Unpack apple
         LDX #$0380      ; Bytes: 896
;        LDY #$6000      ; VRAM pointer
         JSR C3F050      ; Upload apple
         JSR C3E9B3      ; Create apple
         LDA #$03        ; Actor: Shadow
         JSR C3E0B9      ; Init BGs/font
         JMP C3E12X      ; Init text, etc.

; Set to fade in non-glint sprites on table
C3E28A:  LDY #$31C9      ; To OBJ pal 4
         LDX #$9774      ; C2/9774
         JMP c2slw2      ; Queue fade-in

; Set to fade sprite palette 4
C3E29E:  LDY #$31C9      ; To OBJ pal 4
         BRA C3E25_      ; Queue fade-out

; Various: Fade in actor's pseudonym
C3E2B2:  LDY $20         ; Delay expired?
         BNE C3E3A2      ; Exit if not
;        LDA #$F0        ; Switch delay
;        STA $20         ; Set for names
         JSR C3E37_      ; Mode +1, etc.
C3E1DF:  LDY #$3049      ; To BG pal 0
         JSR C3E238      ; Fade name
         LDY #$3169      ; To OBJ pal 1
         BRA C3E238      ; Fade "as"

; Various: Switch actor's names
C3E2C1:  LDY $20         ; Delay expired?
         BNE C3E3A2      ; Exit if not
         INC $26         ; Scene mode +1
         LDY #$0168      ; Frames: 360
         STY $20         ; Set next delay
C3E206:  LDY $00         ; Frames: 0
         STY $CF         ; Set scroll timer
         LDY #$00F0      ; Frames: 240
         STY $64         ; Set scroll limit
         LDY #C3D1B6     ; C3/D1B6
         JSR qanim0      ; Queue scroll fn
         JSR C3E241      ; Fade pseudonym
         LDY #$3169      ; To OBJ pal 1
         JSR black4      ; Fade "as"
C3E22D:  LDY #$3149      ; To OBJ pal 0
C3E238:  LDX #$9754      ; C2/9754
         JMP c2slw4      ; Fade in true name

; Various: Fade all but actor's name, pan table
C3E2D0:  LDY $20         ; Delay expired?
         BNE C3E3A2      ; Exit if not
         JSR C3E845      ; Scroll table

; Various: Fade all but actor's name
C3E337:  LDY $20         ; Delay expired?
         BNE C3E3A2      ; Exit if not
         JSR C3E7B6      ; Fade BGs, etc.
         LDA #$38        ; C3/E2E7
         STA $26         ; Next: Fd name
         BRA C3E29E      ; Fade sprites

; 38: Fade actor's true name
C3E2E7:  LDY $20         ; Delay expired?
         BNE C3E5F4      ; Exit if not
C3E2EB:  JSR C3E5EF      ; Set exit delay
         JSR C39EAE      ; Next: Exit
C3E255:  LDY #$3149      ; To OBJ pal 0
C3E25_:  JMP black4      ; Queue fade-out

; 44, 68: Delete Edgar or Locke's name
C3E3A2:  LDY $20         ; Delay expired?
         BNE C3E5F4      ; Exit if not
         INC $47         ; Erase name: Y
         LDA #$B4        ; Switch delay
C3E3A_:  INC $26         ; Scene mode +1
         BRA C3E5F2      ; For next actor

; 40: Start Edgar and Sabin's scene
C3E34B:  JSR C3EF87      ; Unpack coin
;        LDY #$6000      ; VRAM pointer
         JSR C3F050      ; Upload coin
         JSR C3E28A      ; Fade in coin
         JSR C3EC24      ; Queue coin fn
         LDA #$04        ; Actor: Edgar
         JMP C3E0B_      ; Init BGs/font

; 42, 66: Switch Locke or Edgar's names
C3E373:  LDY $20         ; Delay expired?
         BNE C3E5F4      ; Exit if not
         JSR C3E206      ; Switch names
C3E37_:  INC $26         ; Scene mode +1
         LDA #$F0        ; Intro delay
         BRA C3E5F2      ; For next actor

; 43: Show Sabin's name, fade Edgar's
C3E382:  LDY $20         ; Delay expired?
         BNE C3E5F4      ; Exit if not
         LDA #$05        ; Actor: Sabin
         BRA C3E5DF      ; Branch below

; 67: Show Celes's name, fade Locke's
C3E5D5:  LDY $20         ; Delay expired?
         BNE C3E5F4      ; Exit if not
         LDA #$06        ; Actor: Celes
C3E5DF:  STA $28         ; Set actor slot
;        LDY $00         ; Clear Y
         STY $3F         ; BG3 Y-Pos: 0
         JSR C3E897      ; Create "as"
         JSR C3E255      ; Fade Locke
         JSR C3DF5F      ; Create Celes
         JSR C3E1DF      ; Fade in Celes
C3E5ED:  INC $26         ; Scene mode +1
C3E5EF:  LDA #$78        ; Removal delay
C3E5F2:  STA $20         ; Set for Locke
C3E5F4:  RTS

; 45: Switch Sabin's names
C3E3B2:  LDY $20         ; Delay expired?
         BNE C3E5F4      ; Exit if not
         JSR C3E605      ; Switch names
         LDA #$4F        ; C3/E3CD
         STA $26         ; Next: Fade BG
         RTS

; 32: Start Cyan's scene
C3E2F8:  LDA #$02        ; Actor: Cyan
         JSR C3E0B9      ; Init BGs/font
         JSR C3F036      ; Load glint GFX
         PEA $0226       ; Tiles: 19x2
         LDX #$03E0      ; Src: 7F/03E0
         LDY #$3BA1      ; Dst: 7E/3BA1
         JSR C3EDCD      ; Draw katana
         JSR C3E468      ; Fade in glint
         LDY #$FFB8      ; X: -72 = 440
         STY $35         ; Set BG1 X-Pos
         JSR C3E839      ; Scroll table
         LDY #$04C8      ; 4 glints, X=200
         JSR C3EC2D      ; Queue glints
         JMP C3E123      ; Create text

; 3C: Start Mog's scene
C3E3E1:  LDA #$0A        ; Actor: Mog
         JSR C3E0B9      ; Init BGs/font
         JSR C3F036      ; Unpack sprites
         PEA $080E       ; Tiles: 7x8
         LDX #$0020      ; Src: 7F/0020
         LDY #$3A77      ; Dst: 7E/3A77
         JSR C3EDCD      ; Draw tree
         JSR C3EA2X      ; Create key
         JSR C3EA6C      ; Set to follow BG1
         JSR C3EA2Y      ; Create moogles
         JMP C3E12X      ; Init text, etc.

; 46: Start Gogo's scene
C3E432:  LDA #$0C        ; Actor: Gogo
         JSR C3E0B9      ; Init BGs/font
         JSR C3F036      ; Upload eye GFX
         PEA $091C       ; Tiles: 14x9
         LDX #$002E      ; Src: 7F/002E
         LDY #$3A2D      ; Dst: 7E/3A2D
         JSR C3EDCD      ; Draw helmet
         JSR C3E839      ; Scroll table
         JSR C3E9E4      ; Create pink eye
         JSR C3EA6C      ; Set to follow BG1
         LDY #C3E49F     ; C3/E49F
         JSR qanim2      ; Queue eye fn
         LDA #$B4        ; Frames: 180
         STA $7E3349,X   ; Set eye delay
         JMP C3E123      ; Create text

; Set to fade in glint on table object
C3E468:  LDX #$955C      ; C2/955C
C3E46B:  LDA #$C2        ; Bank: C2
C3E46D:  STA $ED         ; Set src HB
         LDA #$01        ; Slowness: 1
         LDY #$31C9      ; To OBJ pal 4
         JMP C30FAA      ; Queue fade-in

; Animate eye in Gogo's helmet
C3E49F:  CMP #$02        ; Mode 1?
         BEQ C3E4BA      ; Branch if so
         CMP #$06        ; Mode 3?
         BEQ C3E4DF      ; Branch if so

; Modes 0 and 2: Wait
C3E4AB:  JMP C3DF4F      ; Mode timer -1

; Mode 1: Fade in eye
C3E4BA:  INC $3649,X     ; Mode +1
         LDA #$3C        ; Delay: 60
         STA $3349,X     ; Set fade-out's
         LDX #$957C      ; C2/957C
         JSR C3E46B      ; Queue fade-in
         SEC             ; Set to requeue
         RTS

; Mode 3: Fade out eye
C3E4DF:  LDA #$C3        ; C3/10F4
         LDX #C310F4     ; Black palette
         JSR C3E46D      ; Queue fade-out
         CLC             ; Set to dequeue
         RTS

; 50: Start Gau's scene
C3E4FA:  LDA #$0B        ; Actor: Gau
         JSR C3E0B9      ; Init BGs/font
         LDX #$A4E5      ; Source LBs
;        LDA #$D9        ; Source HB
         JSR C3D6C_      ; Unpack face
         LDX #$000C      ; Bytes: 192
         LDY #$6000      ; VRAM pointer
         JSR C3DD86      ; Upload face
         PEA $070C       ; Tiles: 6x7
         LDX #$004A      ; Src: 7F/004A
         LDY #$3AB7      ; Dst: 7E/3AB7
         JSR C3EDCD      ; Draw helmet
         JMP C3E12X      ; Init text, etc.

; 52: Switch Gau's names; create face
C3E522:  LDY $20         ; Delay expired?
         BNE C3EA23      ; Exit if not
         JSR C3E994      ; Create face
         JSR C3EA6C      ; Set to follow BG1
         JMP C3E2C1      ; Switch names

; 64: Start Locke and Celes's scene
C3E59C:  LDA #$01        ; Actor: Locke
         JSR C3E0B9      ; Init BGs/font
         PEA $0618       ; Tiles: 12x6
         LDX #$0274      ; Src: 7F/0274
         LDY #$3B69      ; Dst: 7E/3B69
         JSR C3EDCD      ; Draw bouquet
         LDY #$FFE0      ; X: -32 = 480
         STY $35         ; Set BG1 X-Pos
         JMP C3E12Y      ; Init text; pan

; 69: Switch Celes's names
C3E605:  LDY $20         ; Delay expired?
         BNE C3EA23      ; Exit if not
         JSR C3E3A2      ; Set fade delay
         STZ $47         ; Erase name: N
         LDY #$0014      ; Frames: 20
         STY $85         ; Set scroll delay
         JSR C3E022      ; Create true name
         JMP C3E206      ; Switch names

; 5A: Start Terra's scene
C3E54B:  TDC             ; Actor: Terra
         JSR C3E0B9      ; Init BGs/font
         JSR C3F036      ; Load glint GFX
         PEA $050E       ; Tiles: 7x5
         LDX #$0220      ; Src: 7F/0220
         LDY #$3B35      ; Dst: 7E/3B35
         JSR C3EDCD      ; Draw glasses
         JSR C3E468      ; Fade in glint
         JMP C3E12Y      ; Init text; pan

; 5C: Switch Terra's names, create glint
C3E573:  LDY $20         ; Delay expired?
         BNE C3EA23      ; Exit if not
         JSR C3E2C1      ; Switch names

; Fork: Create glint on pendant
C3EA03:  PEA $6080       ; Pos: 128, 96
         PEA $F5B4       ; Anim table ptr
C3EA0U:  JSR C3E9D2      ; Queue anim fn
C3EA0V:  SEP #$20        ; 8-bit A
         LDA #$CF        ; Bank: CF
C3EA0W:  JSR C3D23A      ; Set ptr HB
         REP #$20        ; 16-bit A
         PLA             ; Anim table ptr
         JSR setaniY     ; Set sprite's
C3EA0X:  PLA             ; X position
C3EA0Y:  JSR C30940      ; Set sprite's
         PLA             ; Y position
C3EA0Z:  STA $7E344A,X   ; Set sprite's
C3EA23:  RTS

; 78: Start Strago's scene
C3E686:  LDA #$07        ; Actor: Strago
         JSR C3E0B_      ; Init BGs/font
         LDX #$8B72      ; Anim table ptr
         BRA C3EDBE      ; Queue book fn

; 2D: Start first book scene
C3E6CE:  JSR C3E0B9      ; Init BGs/font
         JSR C3F023      ; Draw book
         LDY #$02D0      ; Frames: 720
         STY $20         ; Set anim delay
         JMP C3E13D      ; Queue BG1, etc.

; 2E: Animate first book
C3E6DF:  LDY $20         ; Delay expired?
         BNE C3EA23      ; Exit if not
         INC $26         ; Next: C3/E6FA
         LDX #$8B63      ; Anim table ptr
         LDY #$0348      ; Frames: 840
C3EDB_:  STY $20         ; Set fade delay
C3EDBE:  PHX             ; Push anim ptr
         JSR C3F023      ; Draw book
         STZ $99         ; Last page: N
         LDY #C3EE04     ; C3/EE04
         JMP C3D22C      ; Queue anim fn

; 4A: Start last book scene
C3E711:  JSR C3E0B9      ; Init BGs/font
         LDY #$01A0      ; Freeze delay
         LDX #$8B45      ; Anim table ptr
         JSR C3EDB_      ; Queue book fn
         STZ $47         ; Erase text: N
         JSR C3E13D      ; Queue BG1, etc.
         JSR C3E08F      ; Queue "And you"
         PEA $D068       ; Pos: 104, 208
         PEA $F8AB       ; Anim table ptr
         JMP C3EA0V      ; Set object data

; 4B: Stop last book and fade in text
C3E736:  LDY $20         ; Delay expired?
         BNE C3EA23      ; Exit if not
         INC $99         ; Last page: Y
         INC $26         ; Next: C3/E749
         LDY #$016C      ; Frames: 364
         STY $20         ; Set fade delay
         JMP C3E22D      ; Queue fade-in

; 2F: Pixelate and fade first book
C3E6FA:  LDY $20         ; Delay expired?
         BNE C3EA23      ; Exit if not
         JSR C3E845      ; Scroll table

; 4C: Pixelate and fade last book
C3E749:  LDY $20         ; Delay expired?
         BNE C3EA23      ; Exit if not
         LDY #C3E852     ; C3/E852
         JSR qanim0      ; Queue mosaic
         JMP C3E337      ; Fade BGs

; 82: Start Setzer's scene
C3E75D:  JSR C3F036      ; Upload cards
         LDY #$3189      ; To OBJ pal 2
         LDX #$9774      ; C2/9774
         JSR c2slw2      ; Fade in cards
         JSR C3E839      ; Scroll table
         JSR C3E8E3      ; Create cards
         LDA #$09        ; Actor: Setzer
         JMP C3E0B_      ; Init BGs/font

; 6E: Start Relm's scene
C3E635:  LDA #$08        ; Actor: Relm
         JSR C3E0B9      ; Init BGs/font
         JSR C3F036      ; Load glint GFX
         PEA $0214       ; Tiles: 10x2
         LDX #$0360      ; Src: 7F/0360
         LDY #$3BF3      ; Dst: 7E/3BF3
         JSR C3EDCD      ; Draw brush
         JSR C3E468      ; Fade in glint
         LDY #$03E4      ; 3 glints, X=228
         JSR C3EC2D      ; Queue glints
         JMP C3E12Y      ; Init text; pan

; 85: Fade background
C3E7B2:  LDY $20         ; Delay expired?
         BNE C3E001      ; Exit if not
C3E7B6:  JSR C3E5ED      ; Card fade delay
         JMP C3E145      ; Fade BGs

; 86: Fade cards and name
C3E7C1:  LDY $20         ; Delay expired?
         BNE C3E001      ; Exit if not
         JSR C3E2EB      ; Fade name, etc.
         LDY #$3189      ; To OBJ pal 2
         JMP black4      ; Fade cards

; Set to scroll BG1 westward for 20 seconds
C3E839:  LDY #$04B0      ; Frames: 1200
         BRA C3DFED      ; Queue panning

; Set to scroll BG1 westward for 3 seconds
C3E845:  LDY #$00B4      ; Frames: 180
C3DFED:  PHY             ; Save timer
         LDY #C3E002     ; C3/E002
         JSR qanim0      ; Queue anim fn
         REP #$20        ; 16-bit A
         PLA             ; Scroll timer
C3DFFB:  STA $7E3349,X   ; Set mode timer
         SEP #$21        ; 8-bit A; requeue
C3E001:  RTS

; 87: Start Umaro's scene
C3E7E5:  LDA #$0D        ; Actor: Umaro
         JSR C3E0B9      ; Init BGs/font
         JSR C3F036      ; Upload moogles
         JSR C3EF87      ; Unpack skull
         LDY #$6800      ; VRAM pointer
         JSR C3DDB_      ; Upload skull
         PEA $050A       ; Tiles: 5x5
         LDX #$0056      ; Src: 7F/0056
         LDY #$3B37      ; Dst: 7E/3B37
         JSR C3EDCD      ; Draw bones
         LDX #$0013      ; Table index
.loop1   LDA.L moogle,X  ; Ptr or X pos
         PHA             ; Memorize it
         DEX             ; Index -1
         BPL .loop1      ; Loop till < 0
         LDA #$04        ; Moogles: 4
         STA $E8         ; Set counter
.loop2   LDY #C3EAF_     ; C3/EAF_
         JSR qanim2      ; Queue moogle
         PLA             ; Base X
         JSR C30940      ; Set moogle's
         REP #$20        ; 16-bit A
         PLA             ; Bump anim ptr
         STA $7E37C9,X   ; Set moogle's
         PLA             ; Walk timer
         JSR C3DFFB      ; Set moogle's
         DEC $E8         ; One less moogle
         BNE .loop2      ; Loop till last
         JSR C3E12X      ; Init text, etc.
         JSR C3E9D2      ; Queue skull
         JSR C3EA6C      ; Follow BG1: Y
         PEA $4FC4       ; Pos: 196, 79
         PEA $F56E       ; Anim table ptr
         JMP C3EA0V      ; Set object data

; Handle mosaic effect for book
C3E852:  BNE C3E865      ; If mode 1

; Mode 0: Initialize mosaic
C3E85A:  INC $3649,X     ; Mode +1
         STZ $33CA,X     ; Intensity: 0
C3E871:  LDA $33CA,X     ; Intensity
         ORA #$0F        ; Layers: All
         STA $B5         ; Set mosaic
         INC A           ; Intensity +1
         STA $33CA,X     ; Set next one
         LDA #$10        ; Frames: 16
         BRA C3DFFB      ; Set next wait

; Mode 1: Sustain mosaic
C3E865:  LDA $3349,X     ; Delay expired?
         BEQ C3E871      ; Proceed if so
         JMP C3DF59      ; Delay -1

; Queue regular permanent sprite
C3E9D2:  LDY #C3DEAB     ; C3/DEAB
         JMP qanim3      ; Queue OAM fn

; Set sprite to pan with BG1, define Y position
C3EA68:  JSR C3EA0Z      ; Set Y position
C3EA6C:  LDA #$01        ; Pans with BG1
C3EA6E:  STA $7E364A,X   ; Set sprite flags
         RTS

; Create "as"
C3E897:  LDY #C3DE84     ; C3/DE84
         JSR qanim3      ; Queue anim fn
         PEA $C079       ; Pos: 121, 192
         PEA $8E38       ; Anim table ptr
         REP #$20        ; 16-bit A
         LDA #$0258      ; Frames: 600
         JSR C3DFFB      ; Set hide delay
C3E8AF:  SEP #$20        ; 8-bit A
         LDA #$C2        ; Anim ptr HB
         JMP C3EA0W      ; Set object data

; Create face in Gau's diving helmet
C3E994:  PEA $5FCB       ; Pos: 203, 95
         PEA $F647       ; Tbl: CF/F647
         BRA C3E9E_      ; Queue anim fn

; Create Shadow's apple
C3E9B3:  JSR C3E9D2      ; Queue anim fn
         JSR C3EA6C      ; Follow BG1: Y
         PEA $57C0       ; Pos: 192, 87
         PEA $9497       ; Tbl: C2/9497
         BRA C3E8AF      ; Set object data

; Create glowing eye in Gogo's helmet
C3E9E4:  PEA $61C8       ; Pos: 200, 97
         PEA $F450       ; Tbl: CF/F450
C3E9E_:  JMP C3EA0U      ; Queue anim fn

; Create four playing cards
C3E8E3:  LDX #$5078      ; Pos: 120, 80
         LDY #$0020      ; H-Spd: +0.125 px
         JSR C3E97_      ; Queue card 1
         LDX #$C048      ; Pos: 72, 192
         LDY #$0040      ; H-Spd: +0.25 px
         LDA #$60        ; V-Spd: +0.375 px
         JSR C3E97A      ; Queue card 2
         LDX #$1098      ; Pos: 152, 16
         LDY #$FFE0      ; H-Spd: -0.125 px
         JSR C3E97_      ; Queue card 3
         LDX #$80D0      ; Pos: 208, 128
         LDY #$00A0      ; H-Spd: +0.625 px
C3E97_:  LDA #$80        ; V-Spd: +0.5 px
C3E97A:  PHX             ; Save position
         PHY             ; Save H-Speed
         PHA             ; Save V-Speed
         LDY #C3DEAB     ; C3/DEAB
         JSR qanim0      ; Queue anim fn
;        TDC             ; Clear A
         PLA             ; V-Speed
         REP #$20        ; 16-bit A
         STA $7E3549,X   ; Set card's
         PLA             ; H-Speed
         STA $7E34C9,X   ; Set card's
         PEA $92FA       ; Tbl: C2/92FA
         BRA C3E8AF      ; Set object data

; Create Mog's winding key
C3EA2X:  PEA $6FE0       ; Pos: 224, 111
         PEA $F515       ; Tbl: CF/F515
         BRA C3E9E_      ; Queue anim fn

; Create Mog's mini moogle
C3EA2Y:  LDY #$64BA      ; Pos: 186, 100
         JSR C3EA2_      ; Create moogle 1
         LDY #$64C6      ; Pos: 198, 100
         JSR C3EA2_      ; Create moogle 2
         LDY #$64D1      ; Pos: 209, 100
         JSR C3EA2_      ; Create moogle 3
         LDY #$64DC      ; Pos: 220, 100
C3EA2_:  PHY             ; Save position
         PEA $F458       ; Tbl: CF/F458
         LDY #C3EAFC     ; C3/EAFC
         JSR qanim2      ; Queue anim fn
         JMP C3EA0V      ; Set object data

; Animate mini moogle for Mog's scene
C3EAFC:  CMP #$02        ; Mode 1?
         BEQ C3EB35      ; Branch if so
         CMP #$04        ; Mode 2?
         BEQ C3EB5E      ; Branch if so
         BCS C3EB7B      ; If mode 3

; Mode 0: Initialize data
C3EB10:  JSR C3EA6C      ; Follow BG1: Y
         REP #$20        ; 16-bit A
         LDA #$0168      ; Frames: 360
         STA $3349,X     ; Set spin timer
         JSR C3120_      ; Set pose timer

; Mode 1: Spin around
C3EB35:  LDY $3349,X     ; Mode expired?
         BNE C3EBC8      ; Branch if not
         LDA #$10        ; Frames: 16
         STA $3349,X     ; Set jump timer
         REP #$20        ; 16-bit A
         LDA #$F47C      ; Anim table ptr
         BRA C3EBBB      ; Scroll & OAM

; Mode 2: Jump down
C3EB5E:  LDY $3349,X     ; Mode expired?
         BNE C3EB6D      ; Branch if not
         INC $3649,X     ; Mode +1
         LDA #$3C        ; Frames: 60
         STA $3349,X     ; Set walk delay
C3EB6D:  LDA $35C9,X     ; Modifier index
         TAX             ; Index it
         LDA $CFF55E,X   ; Y modifier
         LDX $2D         ; Queue index
         CLC             ; Prepare ADC
         ADC $344A,X     ; Add OBJ's Y
         STA $344A,X     ; Set OBJ's Y
         INC $35C9,X     ; Y-Pos mod +1
         BRA C3EBC8      ; Scroll & OAM

; Mode 3: Face left
C3EB7B:  LDY $3349,X     ; Mode expired?
         BNE C3EBC8      ; Branch if not
         JSR C3EBD5      ; Set to walk
         BRA C3EBC8      ; Scroll & OAM

; Animate moogle for Umaro's scene
C3EAF_:  CMP #$02        ; Mode 1?
         BEQ C3EBA9      ; Branch if so
         BCS C3EBD0      ; If mode 2

; Mode 0: Initialize data
C3EB9C:  JSR C3EBD5      ; Set to walk
         LDA #$70        ; Y: 112
         JSR C3EA68      ; Follow BG1: Y

; Mode 1: Walk toward bones
C3EBA9:  LDY $3349,X     ; Mode expired?
         BNE C3EBC8      ; Branch if not
         REP #$20        ; 16-bit A
         STZ $34C9,X     ; H-Speed: 0
         LDA $37C9,X     ; Queue anim ptr
C3EBBB:  STA $32C9,X     ; Set as current
         JSR C3120_      ; Set pose timer
C3EBC8:  JSR C3CFE5      ; Mode timer -1

; Mode 2: Stop and fall down
C3EBD0:  JSR C3DEC0      ; Scroll & OAM
         SEC             ; Set to requeue
C3EBD4:  RTS

; Set mini moogle to walk eastward
C3EBD5:  LDA #$40        ; H-Spd: +0.25 px
         STA $34C9,X     ; Set sprite's
         STZ $34CA,X     ; Clear HB
         LDY #$F473      ; Anim ptr LBs
C3EBD_:  LDA #$CF        ; Anim ptr HB
         JMP initob2     ; Set pose timer

; Create double-headed coin
C3EC24:  LDY #C3ED39     ; C3/ED39
         JSR qanim2      ; Queue anim fn
         PEA $6410       ; Pos: 16, 100
         PEA $EC93       ; Anim table ptr
         JMP C3859Y      ; Set object data

; Animate double-headed coin
C3ED39:  BNE C3ED6D      ; If mode 1

; Mode 0: Initialize sprite
C3ED41:  LDA #$80        ; H-Spd: +0.5 px
         STA $34C9,X     ; Set sprite's
         LDA #$C8        ; Frames: 200
         STA $3349,X     ; Set roll timer
         JSR C3120_      ; Set pose timer

; Mode 1: Sustain sprite
C3ED6D:  LDA $3349,X     ; Stop rolling?
         BNE C3ED77      ; Branch if not
         STZ $34C9,X     ; H-Speed: 0
C3ED77:  DEC $3349,X     ; Roll timer -1
C3ED7A:  BRA C3EBD0      ; Scroll & OAM

; Set to create glints on brush or katana
C3EC2D:  STY $C7         ; Glint count; X
         LDY #C3EC48     ; C3/EC48
         JSR qanim0      ; Queue anim fn
         REP #$20        ; 16-bit A
         LDA #$0168      ; Frames: 360
         JMP C3DFFB      ; Set wave delay

; Create a series of glints on brush or katana
C3EC48:  CLC             ; Preset to dequeue
         LDA $C8         ; Glints left
         BEQ C3EBD4      ; Dequeue if 0
         LDY $3349,X     ; Delay expired?
         BNE C3ECA7      ; Branch if not
         LDA #$08        ; Frames: 8
         STA $3349,X     ; Set next delay
         JSR C3EC3F      ; Queue glint
         DEC $C8         ; Glints left -1
C3ECA7:  JMP C3CFE5      ; Delay -1; requeue

; Queue brush or katana glint
C3EC3F:  LDA $26         ; Current scene
         CMP #$6E        ; C+ if Relm's
         LDY #$F5B4      ; Ptr for brush
         LDA #$68        ; Y: 104
         BCS .skip       ; If Relm's scene
         LDY #$F5FA      ; Ptr for katana
         LDA #$60        ; Y: 96
.skip    PHA             ; Push Y position
         LDA $C7         ; Base X
         PHA             ; Put on stack
         DEC A           ; Base X -1
         DEC A           ; Base X -1
         DEC A           ; Base X -1
         DEC A           ; Base X -1
         STA $C7         ; Save changes
         PHY             ; Push anim ptr
         LDY #C3ED14     ; C3/ED14
         JSR qanim2      ; Queue anim fn
         JMP C3EA0V      ; Set object data

; Animate glint on brush or katana
C3ED14:  BNE C3ED29      ; If mode 1

; Mode 0: Initialize sprite
C3ED1C:  JSR C3EA6C      ; Follow BG1: Y
         JSR C3120_      ; Set pose timer

; Mode 1: Sustain sprite
C3ED29:  LDA $36CA,X     ; Endless pose?
         BPL C3ED7A      ; Branch if not
         CLC             ; Set to dequeue
         RTS

; Animate book
C3EE04:  CMP #$02        ; Mode 1?
         BEQ C3EE18      ; Branch if so
         BCS C3EE1C      ; If mode 2

; Mode 0: Initialize timer
C3EE0E:  JSR C3120_      ; Set pose timer

; Mode 1: Check if last page
C3EE18:  LDA $99         ; Last page?
         BEQ C3EE1C      ; Branch if not
;        JSR C3D239      ; Ptr HB: C2
         LDY #$8B54      ; Anim ptr LBs
         JSR initob3     ; Set pose timer

; Mode 2: Update tilemap
C3EE1C:  JSR ld36C_      ; Pose info loc
         JSR E7_35CA     ; Set source
         LDY #$0007      ; Src index: 7
.loop    LDA [$E7],Y     ; Pose data
         PHA             ; Memorize it
         DEY             ; Src index -1
         BPL .loop       ; Loop till < 0
         XBA             ; Save width
         PLX             ; Unstack it
         PLX             ; Map height
         TXA             ; Put in A
         PLX             ; Source LBs
         PLY             ; Dest LBs
         PHA             ; Push height
         XBA             ; Map width
         PHA             ; Put on stack
         JSR C3EDCD      ; Replace tiles
         RTS             ; ^ Avoid JMP

; Unpack and upload large font graphics for actor scene
C3EE74:  JSR bigfont     ; Unpack GFX
         LDX #$0090      ; Bytes: 2304
C3EE8A:  LDY #$7000      ; VRAM pointer

; Convert and upload graphics for range of font characters
C3DD86:  STY $2116       ; Set VRAM ptr
C3DD8D:  REP #$20        ; 16-bit A
         LDY #$0008      ; 8-px rows: 8
         PHY             ; Save this value
C3DD90:  LDA [$E7]       ; 8 px, 2 planes
         STA $2118       ; Save in VRAM
         INC $E7         ; Source +1
         INC $E7         ; Source +1
         DEY             ; One less row
         BNE C3DD90      ; Loop till last
         PLY             ; 8-px rows: 8
         JSR C3DD7A      ; Clear plane C+D
         DEX             ; One less tile
         BNE C3DD8D      ; Loop till last
         RTS

; Unpack and upload BG graphics and tilemaps for actor scene
C3EE90:  LDX #$C008      ; Source LBs
;        LDA #$C4        ; Source HB
         JSR bigfon_     ; Unpack data

; Fork: Copy table's map to BG1 tilemap A
C3EE98:  LDY #$3849      ; BG1 map A
         JSR C3EF10      ; Move data

; Fork: Copy table's map to BG1 tilemap B
C3EEA4:  LDY #$4049      ; BG1 map B
         JSR C3EF10      ; Move data

; Fork: Upload tall items and table side graphics
C3EEB0:  JSR C3D518      ; Src: 7E/C000
         LDX #$1F60      ; Bytes: 8032
         LDY #$3000      ; VRAM pointer
         JSR C3DDB_      ; Upload data

; Fork: Upload large items, tabletop, and overlay graphics
C3EEC8:  LDY #$DF60      ; 7E/DF60
         JSR C3D51B      ; Define source
         LDX #$0B40      ; Bytes: 2880
         LDY #$4000      ; VRAM pointer
         JSR C3DDB_      ; Upload data

; Fork: Upload book graphics
C3EEE0:  LDY #$EAA0      ; 7E/EAA0
         JSR C3D51B      ; Define source
         LDX #$0E00      ; Bytes: 3584
         LDY #$5000      ; VRAM pointer
         JSR C3DDB_      ; Upload data

; Fork: Upload transparent overlay tilemap (BG2)
C3EEF8:  LDY #$0448      ; 7F/0448
         LDA #$7F        ; Bank: 7F
         JSR C3D51D      ; Define source
         LDX #$0780      ; Bytes: 1920
         LDY #$1000      ; VRAM pointer
         BRA C3F053      ; Upload data

; Relocate decompressed tilemap for table
C3EF10:  STY $EB         ; Set dst LBs
         LDY #$F8A0      ; Src: 7E/F8A0
         JSR C3D51B      ; Define source
;        LDA #$7E        ; Bank: 7E
         STA $ED         ; Set dst HB
         LDY #$077F      ; Bytes: 1920
C3EF1C:  LDA [$E7],Y     ; Unpacked data
         STA [$EB],Y     ; Put in buffer
         DEY             ; Index -1
         BPL C3EF1C      ; Loop till < 0
         RTS

; Unpack skull and coin graphics; prepare upload
C3EF87:  LDX #$9D4B      ; Source LBs
;        LDA #$D9        ; Source HB
         JSR C3D6C_      ; Unpack GFX
         LDX #$0800      ; Bytes: 896
         RTS

; Unpack and upload card, glint, moogle, and winding key graphics
C3F036:  LDX #$F6FB      ; Source LBs
;        LDA #$C4        ; Source HB
         JSR bigfon_     ; Unpack GFX
         LDX #$0DF0      ; Bytes: 3568
C3F050:  LDY #$6000      ; VRAM pointer
C3F053:  JMP C3DDB_      ; Upload GFX

; Draw an open book
C3F023:  PLX             ; Return address
         PEA $061C       ; Tiles: 14x6
         PHX             ; Repush address
         LDX #$0BC8      ; Src: 7F/0BC8
         LDY #$3AD9      ; Dst: 7E/3AD9

; Integrate decompressed tilemap segment
C3EDCD:  JSR C3D51B      ; Set dest
         STX $EB         ; Set src LBs
         INC A           ; Bank: 7F
         STA $ED         ; Set src HB
         PLX             ; Return address
         PLY             ; Tilemap size
         STY $E0         ; Memorize it
         PHX             ; Repush address
C3EDDD:  TDC             ; Clear A
         LDA $E0         ; Last column
         DEC A           ; Rectify value
         TAY             ; Index it
         JSR C30F9C      ; Insert a row
         LDA #$40        ; Loop counter
         REP #$20        ; 16-bit A
.rowdown INC $E7         ; Dest +1
         INC $EB         ; Source +1
         DEC A           ; Counter -1
         BNE .rowdown    ; Loop till 0
         SEP #$21        ; 8-bit A; requeue
         DEC $E1         ; One less row
         BNE C3EDDD      ; Loop till last
         RTS

; =============================[ D-Pad handlers ]==============================

; Handle D-Pad for Save menu
C315AA:  JSR C3072D      ; Handle D-Pad
C315AD:  LDY #C315CE     ; C3/15CE
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for Load menu
C315BE:  JSR C3072D      ; Handle D-Pad
C315C1:  LDY #C315CC     ; C3/15CC
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for save file confirmation
C315DA:  JSR C3072D      ; Handle D-Pad
C315DD:  LDY #C315E8     ; C3/15E8
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for non-shifted Status menu
C33705:  JSR C3072D      ; Handle D-Pad
C33708:  LDY #C33713     ; C3/3713
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for Config page 1
C3385E:  JSR C3072D      ; Handle D-Pad
C33861:  LDY #C3386C     ; C3/386C
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for Config page 2
C33884:  JSR C3072D      ; Handle D-Pad
C33887:  LDY #C33892     ; C3/3892
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for Cmd.Set menu's actor list
C345BC:  JSR C3072D      ; Handle D-Pad
C345BF:  LDY #C345CA     ; C3/45CA
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for Controller menu
C348DD:  JSR C3072D      ; Handle D-Pad
C348E0:  LDY #C348EB     ; C3/48EB
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for SwdTech, Blitz, or Dance menu
C34BD_:  JSR dsclist     ; Queue list; desc on
C34BD4:  JSR C3072D      ; Handle D-Pad
C34BD7:  LDY #C34BE4     ; C3/4BE4
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for esper data menu
C35983:  JSR C3072D      ; Handle D-Pad
C35986:  LDY #C35991     ; C3/5991
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for shifted Status menu
C36409:  JSR C3072D      ; Handle D-Pad
C3640C:  LDY #C36417     ; C3/6417
C3640F:  JMP C30640      ; Relocate cursor

; Handle D-Pad for actor naming menu
C3663E:  JSR C3072D      ; Handle D-Pad
C36641:  LDY #C3664C     ; C3/664C
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for rare item menu
C37D4A:  JSR C3072D      ; Handle D-Pad
C37D4D:  LDY #C37D58     ; C3/7D58
         BRA C3640F      ; Relocate cursor

; Relocate cursor for Item menu list
C37D86:  LDY #C37D9D     ; C3/7D9D
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for Equip menu options
C38E56:  JSR C3072D      ; Handle D-Pad
C38E59:  LDY #C38E64     ; C3/8E64
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for Equip menu slot selection
C38E72:  JSR C3072D      ; Handle D-Pad
C38E75:  LDY #C38E80     ; C3/8E80
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for Relic menu options
C38EC3:  JSR C3072D      ; Handle D-Pad
C38EC6:  LDY #C38ED1     ; C3/8ED1
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for relic slot selection
C38EDB:  JSR C3072D      ; Handle D-Pad
C38EDE:  LDY #C38EE9     ; C3/8EE9
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for final battle lineup
C3AC5A:  JSR C3072D      ; Handle D-Pad
C3AC5D:  LDY #C3AC68     ; C3/AC68
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for Colosseum matchup menu
C3B329:  JSR C3072D      ; Handle D-Pad
C3B32C:  LDY #C3B337     ; C3/B337
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for main shop menu
C3B88C:  JSR C3072D      ; Handle D-Pad
C3B88F:  LDY #C3B89A     ; C3/B89A
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for Buy menu
C3B8A6:  JSR C3072D      ; Handle D-Pad
C3B8A9:  LDY #C3B8B4     ; C3/B8B4
         BRA C3640F      ; Relocate cursor

; Handle D-Pad for Lore menu
C34BFA:  JSR C381C7      ; Handle D-Pad
C34BFD:  LDY #C34C08     ; C3/4C08
         BRA C37D28      ; Relocate cursor

; Handle D-Pad for Espers menu
C34C1E:  JSR C381C7      ; Handle D-Pad
C34C21:  LDY #C34C2C     ; C3/4C2C
         BRA C37D28      ; Relocate cursor

; Handle D-Pad for Rage menu
C34C52:  JSR C381C7      ; Handle D-Pad
C34C55:  LDY #C34C60     ; C3/4C60
         BRA C37D28      ; Relocate cursor

; Handle D-Pad for Item, Colosseum, or Sell menu
C37D22:  JSR C381C7      ; Handle D-Pad
C37D25:  LDY #C37D30     ; C3/7D30
C37D28:  JMP C30648      ; Relocate cursor

; Handle D-Pad and cursor memory for Skills menu
C34B56:  JSR C3072D      ; Handle D-Pad
C34B59:  LDY #C34B74     ; C3/4B74
         JSR C30640      ; Relocate cursor
         JSR ld28x2      ; X = Mbr slot
         REP #$20        ; 16-bit A
         LDA $4D         ; Cursor position
         STA $0236,X     ; Remember it
         SEP #$20        ; 8-bit A
         RTS

; Handle D-Pad and cursor memory for Magic menu
C34B88:  JSR C381C7      ; Handle D-Pad
C34B8B:  LDY #C34BAE     ; C3/4BAE
         JSR C30648      ; Relocate cursor
         JSR ld28        ; X = Mbr slot
         ASL A           ; Member slot x2
         TAY             ; Index it
         LDA $4A         ; Scroll position
         STA $0246,X     ; Remember it
         LDA $4F         ; List column
         STA $023E,Y     ; Remember it
         LDA $50         ; List row
         STA $023F,Y     ; Remember it
         RTS

; ========================[ Navigation data loading ]==========================

; Load nav data for Colosseum matchup or Equip menu options
C3B323:  LDY #C3B332     ; C3/B332
         JSR C305FE      ; Load navig data
         JMP C38E59      ; Relocate cursor

; Load nav data for main shop menu or Item menu options
C3B886:  LDY #C3B895     ; C3/B895
         JSR C305FE      ; Load navig data
         JMP C3B88F      ; Relocate cursor

; Load navigation data for Load menu, Controller menu,
; non-shifted Status menu, or Equip menu slot selection
C315B8:  LDY #C315C7     ; C3/15C7
         BRA C305FE      ; Load navig data

; Load nav data for save file confirmation or relic slot selection
C315D4:  LDY #C315E3     ; C3/15E3
         BRA C305FE      ; Load navig data

; Load navigation data for gear list or Config page 1
C33858:  LDY #C33867     ; C3/3867
         BRA C305FE      ; Load navig data

; Load navigation data for Cmd.Set menu's actor list
C345B6:  LDY #C345C5     ; C3/45C5
         BRA C305FE      ; Load navig data

; Load navigation data for Skills menu
C34B50:  LDY #C34B6F     ; C3/4B6F
         BRA C305FE      ; Load navig data

; Load navigation data for Magic, Espers, or Rage menu
C34B82:  LDY #C34BA9     ; C3/4BA9
         BRA C305FE      ; Load navig data

; Load navigation data for esper data menu
C3597D:  LDY #C3598C     ; C3/598C
         BRA C305FE      ; Load navig data

; Load navigation data for Relic menu options
C38EBD:  LDY #C38ECC     ; C3/8ECC
         JSR C305FE      ; Load navig data
         JMP C38EC6      ; Relocate cursor

; Load navigation data for non-party area of Lineup menu
C37482:  STZ $4A         ; Cursor area: Top
         LDY #C374BE     ; C3/74BE

; Load navigation parameters for menu area
C305FE:  LDA #$C3        ; Bank: C3
C30600:  STA $ED         ; Set src HB
         STY $EB         ; Set src LBs
         LDY $00         ; Index: 0
         LDA [$EB],Y     ; Wrap flags
         STA $59         ; Save in RAM
         INY             ; Index +1
         REP #$20        ; 16-bit A
         LDA [$EB],Y     ; Initial spot
         STA $4D         ; Set cursor's
         INY             ; Index +1
         INY             ; Index +1
         LDA [$EB],Y     ; Rows, columns
         STA $53         ; Set boundaries
         STZ $4F         ; List col/row: 1
         SEP #$20        ; 8-bit A
         RTS

; =========================[ Navigation parameters ]===========================

; Navigation data for Save menu
C315B3:  db $80          ; Wraps vertically
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $03          ; 3 rows

; Navigation data for save file confirmation and relic slot selection
C315E3:  db $80          ; Wraps vertically
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $02          ; 2 rows

; Navigation data for esper data menu and main menu
C3598C:  db $80          ; Wraps vertically
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $07          ; 7 rows

; Navigation data for gear list and Config page 1
C33867:  db $81          ; Never wraps
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $09          ; 9 rows

; Navigation data for Config page 2
C3388D:  db $81          ; Never wraps
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $06          ; 6 rows

; Navigation data for Cmd.Set menu's actor list
C345C5:  db $80          ; Wraps vertically
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $05          ; 5 rows

; Navigation data for Cmd.Set menu's command list
C3464A:  db $81          ; Never wraps
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $04          ; 4 rows

; Navigation data for Skills menu
C34B6F:  db $80          ; Wraps vertically
         db $00          ; Initial column
         db $01          ; Initial row
         db $01          ; 1 column
         db $07          ; 7 rows

; Navigation data for Magic, Espers, and Rage menus
C34BA9:  db $01          ; Wraps horizontally
         db $00          ; Initial column
         db $00          ; Initial row
         db $02          ; 2 columns
         db $08          ; 8 rows

; Navigation data for Lore menu
C34C03:  db $01          ; Wraps horizontally
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $08          ; 8 rows

; Navigation data for shifted Status menu
C36412:  db $80          ; Wraps vertically
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $10          ; 16 rows

; Navigation data for SwdTech, Blitz, and Dance menus
C34BDF:  db $00          ; Wraps on all sides
         db $00          ; Initial column
         db $00          ; Initial row
         db $02          ; 2 columns
         db $04          ; 4 rows

; Navigation data for actor naming menu
C36647:  db $01          ; Wraps horizontally
         db $00          ; Initial column
         db $00          ; Initial row
         db $0A          ; 10 columns
         db $07          ; 7 rows

; Navigation data for non-party area of Lineup menu
C374BE:  db $81          ; Never wraps
         db $00          ; Initial column
         db $00          ; Initial row
         db $08          ; 8 columns
         db $02          ; 2 rows

; Navigation data for single-party area of Lineup menu
C374E3:  db $81          ; Never wraps
         db $00          ; Initial column
         db $00          ; Initial row
         db $02          ; 2 columns
         db $02          ; 2 rows

; Navigation data for two-party area of Lineup menu
C374F0:  db $81          ; Never wraps
         db $00          ; Initial column
         db $00          ; Initial row
         db $04          ; 4 columns
         db $02          ; 2 rows

; Navigation data for three-party area of Lineup menu
C37505:  db $81          ; Never wraps
         db $00          ; Initial column
         db $00          ; Initial row
         db $06          ; 6 columns
         db $02          ; 2 rows

; Navigation data for Item, Colosseum, and Sell menus
C37D2B:  db $01          ; Wraps horizontally
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $0A          ; 10 rows

; Navigation data for rare item menu
C37D53:  db $00          ; Wraps on all sides
         db $00          ; Initial column
         db $00          ; Initial row
         db $02          ; 2 columns
         db $0A          ; 10 rows

; Navigation data for Relic menu options
C38ECC:  db $01          ; Wraps horizontally
         db $00          ; Initial column
         db $00          ; Initial row
         db $02          ; 2 columns
         db $01          ; 1 row

; Navigation data for final battle lineup
C3AC63:  db $80          ; Wraps vertically
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $0E          ; 14 rows

; Navigation data for Colosseum matchup and Equip menu options
C3B332:  db $01          ; Wraps horizontally
         db $00          ; Initial column
         db $00          ; Initial row
         db $04          ; 4 columns
         db $01          ; 1 row

; Navigation data for main shop menu and Item menu options
C3B895:  db $01          ; Wraps horizontally
         db $00          ; Initial column
         db $00          ; Initial row
         db $03          ; 3 columns
         db $01          ; 1 row

; Navigation data for Buy menu
C3B8AF:  db $80          ; Wraps vertically
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $08          ; 8 rows

; Navigation data for Load menu, Controller menu,
; non-shifted Status menu, Equip menu slot selection,
; and portrait selection
C315C7:  db $80          ; Wraps vertically
         db $00          ; Initial column
         db $00          ; Initial row
         db $01          ; 1 column
         db $04          ; 4 rows

; Cursor positions for portrait selection
C33047:  dw $2808        ; Member A
         dw $5808        ; Member B
         dw $8808        ; Member C
         dw $B808        ; Member D

; Cursor positions above must follow nav table.


; ============================[ Cursor positions ]=============================

; Cursor positions for Save and Load menus
C315CC:  dw $1C08        ; New Game
C315CE:  dw $3C08        ; Save file 1
         dw $7408        ; Save file 2
         dw $AC08        ; Save file 3

; Cursor positions for save file confirmation
C315E8:  dw $44BF        ; Yes
         dw $54BF        ; No

; Cursor positions for main menu
C32F8A:  dw $12AF        ; Item
         dw $21AF        ; Skills
         dw $30AF        ; Equip
         dw $3FAF        ; Relic
         dw $4EAF        ; Status
         dw $5DAF        ; Config
         dw $6CAF        ; Save

; Cursor positions for non-shifted Status menu
C33713:  dw $5990        ; Command 1
         dw $6590        ; Command 2
         dw $7190        ; Command 3
         dw $7D90        ; Command 4

; Cursor positions for Config page 1
C3386C:  dw $2960        ; Bat.Mode
         dw $3960        ; Bat.Speed
         dw $4960        ; Msg.Speed
         dw $5960        ; Cmd.Set
         dw $6960        ; Gauge
         dw $7960        ; Sound
         dw $8960        ; Cursor
         dw $9960        ; Reequip
         dw $A960        ; Controller

; Cursor positions for Config page 2
C33892:  dw $2960        ; Mag.Order
         dw $6960        ; Window
         dw $7960        ; Color
         dw $9960        ; R
         dw $A960        ; G
         dw $B960        ; B

; Cursor positions for Cmd.Set menu's actor list
C345CA:  dw $2008        ; Member A
         dw $5008        ; Member B
         dw $8008        ; Member C
         dw $B008        ; Member D
         dw $0C08        ; Arrange

; Cursor positions for member 1 in Cmd.Set menu
C3464F:  dw $2080        ; Command A
         dw $2C58        ; Command B
         dw $2CA8        ; Command C
         dw $3880        ; Command D

; Cursor positions for member 2 in Cmd.Set menu
C34657:  dw $5080        ; Command 1
         dw $5C58        ; Command 2
         dw $5CA8        ; Command 3
         dw $6880        ; Command 4

; Cursor positions for member 3 in Cmd.Set menu
C3465F:  dw $8080        ; Command 1
         dw $8C58        ; Command 2
         dw $8CA8        ; Command 3
         dw $9880        ; Command 4

; Cursor positions for member 4 in Cmd.Set menu
C34667:  dw $B080        ; Command 1
         dw $BC58        ; Command 2
         dw $BCA8        ; Command 3
         dw $C880        ; Command 4

; Cursor positions for Controller menu
C348EB:  dw $7B50        ; Member A
         dw $8B50        ; Member B
         dw $9B50        ; Member C
         dw $AB50        ; Member D

; Cursor positions for Skills menu
C34B74:  dw $1400        ; Espers
         dw $2400        ; Magic
         dw $4400        ; SwdTech
         dw $5400        ; Blitz
         dw $6400        ; Lore
         dw $7400        ; Rage
         dw $8400        ; Dance

; Cursor positions for Magic menu
C34BAE:  dw $7408        ; Spell 1
         dw $7470        ; Spell 2
         dw $8008        ; Spell 3
         dw $8070        ; Spell 4
         dw $8C08        ; Spell 5
         dw $8C70        ; Spell 6
         dw $9808        ; Spell 7
         dw $9870        ; Spell 8
         dw $A408        ; Spell 9
         dw $A470        ; Spell 10
         dw $B008        ; Spell 11
         dw $B070        ; Spell 12
         dw $BC08        ; Spell 13
         dw $BC70        ; Spell 14
         dw $C808        ; Spell 15
         dw $C870        ; Spell 16

; Cursor positions for Rage menu
C34C60:  dw $7418        ; Rage 1
         dw $7488        ; Rage 2
         dw $8018        ; Rage 3
         dw $8088        ; Rage 4
         dw $8C18        ; Rage 5
         dw $8C88        ; Rage 6
         dw $9818        ; Rage 7
         dw $9888        ; Rage 8
         dw $A418        ; Rage 9
         dw $A488        ; Rage 10
         dw $B018        ; Rage 11
         dw $B088        ; Rage 12
         dw $BC18        ; Rage 13
         dw $BC88        ; Rage 14
         dw $C818        ; Rage 15
         dw $C888        ; Rage 16

; Cursor positions for esper data menu
C35991:  dw $7010        ; Esper
         dw $7C18        ; Spell A
         dw $8818        ; Spell B
         dw $9418        ; Spell C
         dw $A018        ; Spell D
         dw $AC18        ; Spell E
         dw $B818        ; Bonus

; Cursor positions for SwdTech, Blitz, and Dance menus
C34BE4:  dw $7408        ; Skill 1
         dw $7478        ; Skill 2
         dw $8C08        ; Skill 3
         dw $8C78        ; Skill 4
         dw $A408        ; Skill 5
         dw $A478        ; Skill 6
         dw $BC08        ; Skill 7
         dw $BC78        ; Skill 8

; Cursor positions for shifted Status menu
C36417:  dw $10F0        ; Empty
         dw $1CF0        ; Command 1
         dw $28F0        ; Command 2
         dw $34F0        ; Command 3
         dw $40F0        ; Command 4
         dw $4CF0        ; Command 5
         dw $58F0        ; Command 6
         dw $64F0        ; Command 7
         dw $70F0        ; Command 8
         dw $7CF0        ; Command 9
         dw $88F0        ; Command 10
         dw $94F0        ; Command 11
         dw $A0F0        ; Command 12
         dw $ACF0        ; Command 13
         dw $B8F0        ; Command 14
;        dw $C4F0        ; Command 15 (unused)

; Cursor positions for actor naming menu
C3664C:  dw $5838        ; A
         dw $5848        ; B
         dw $5858        ; C
         dw $5868        ; D
         dw $5878        ; E
         dw $5890        ; a
         dw $58A0        ; b
         dw $58B0        ; c
         dw $58C0        ; d
         dw $58D0        ; e
         dw $6838        ; F
         dw $6848        ; G
         dw $6858        ; H
         dw $6868        ; I
         dw $6878        ; J
         dw $6890        ; f
         dw $68A0        ; g
         dw $68B0        ; h
         dw $68C0        ; i
         dw $68D0        ; j
         dw $7838        ; K
         dw $7848        ; L
         dw $7858        ; M
         dw $7868        ; N
         dw $7878        ; O
         dw $7890        ; k
         dw $78A0        ; l
         dw $78B0        ; m
         dw $78C0        ; n
         dw $78D0        ; o
         dw $8838        ; P
         dw $8848        ; Q
         dw $8858        ; R
         dw $8868        ; S
         dw $8878        ; T
         dw $8890        ; p
         dw $88A0        ; q
         dw $88B0        ; r
         dw $88C0        ; s
         dw $88D0        ; t
         dw $9838        ; U
         dw $9848        ; V
         dw $9858        ; W
         dw $9868        ; X
         dw $9878        ; Y
         dw $9890        ; u
         dw $98A0        ; v
         dw $98B0        ; w
         dw $98C0        ; x
         dw $98D0        ; y
         dw $A838        ; Z
         dw $A848        ; !
         dw $A858        ; ?
         dw $A868        ; /
         dw $A878        ; :
         dw $A890        ; z
         dw $A8A0        ; ”
         dw $A8B0        ; ’
         dw $A8C0        ; -
         dw $A8D0        ; .
         dw $B838        ; 0
         dw $B848        ; 1
         dw $B858        ; 2
         dw $B868        ; 3
         dw $B878        ; 4
         dw $B890        ; 5
         dw $B8A0        ; 6
         dw $B8B0        ; 7
         dw $B8C0        ; 8
         dw $B8D0        ; 9

; Cursor positions for non-party area of Lineup menu
C374C3:  dw $6408        ; Slot 1
         dw $6424        ; Slot 2
         dw $6440        ; Slot 3
         dw $645C        ; Slot 4
         dw $6478        ; Slot 5
         dw $6494        ; Slot 6
         dw $64B0        ; Slot 7
         dw $64CC        ; Slot 8
         dw $8008        ; Slot 9
         dw $8024        ; Slot 10
         dw $8040        ; Slot 11
         dw $805C        ; Slot 12
         dw $8078        ; Slot 13
         dw $8094        ; Slot 14
         dw $80B0        ; Slot 15
         dw $80CC        ; Slot 16

; Cursor positions for single-party area of Lineup menu
C374E8:  dw $A408        ; NW
         dw $A428        ; NE
         dw $C008        ; SW
         dw $C028        ; SE

; Cursor positions for two-party area of Lineup menu
C374F5:  dw $A408        ; Party 1, NW
         dw $A428        ; Party 1, NE
         dw $A458        ; Party 2, NW
         dw $A478        ; Party 2, NE
         dw $C008        ; Party 1, SW
         dw $C028        ; Party 1, SE
         dw $C058        ; Party 2, SW
         dw $C078        ; Party 2, SE

; Cursor positions for three-party area of Lineup menu
C3750A:  dw $A408        ; Party 1, NW
         dw $A428        ; Party 1, NE
         dw $A458        ; Party 2, NW
         dw $A478        ; Party 2, NE
         dw $A4A8        ; Party 3, NW
         dw $A4C8        ; Party 3, NE
         dw $C008        ; Party 1, SW
         dw $C028        ; Party 1, SE
         dw $C058        ; Party 2, SW
         dw $C078        ; Party 2, SE
         dw $C0A8        ; Party 3, SW
         dw $C0C8        ; Party 3, SE

; Cursor positions for Item, Colosseum, Sell, and Lore menus
C37D30:  dw $5C08        ; Item 1
         dw $6808        ; Item 2
C34C08:  dw $7408        ; Item 3, Lore 1
         dw $8008        ; Item 4, Lore 2
         dw $8C08        ; Item 5, Lore 3
         dw $9808        ; Item 6, Lore 4
         dw $A408        ; Item 7, Lore 5
         dw $B008        ; Item 8, Lore 6
         dw $BC08        ; Item 9, Lore 7
         dw $C808        ; Item 10, Lore 8

; Cursor positions for Rare and Espers menus
C37D58:  dw $5C08        ; Item 1
         dw $5C78        ; Item 2
         dw $6808        ; Item 3
         dw $6878        ; Item 4
C34C2C:  dw $7408        ; Item 5, Esper 1
         dw $7478        ; Item 6, Esper 2
         dw $8008        ; Item 7, Esper 3
         dw $8078        ; Item 8, Esper 4
         dw $8C08        ; Item 9, Esper 5
         dw $8C78        ; Item 10, Esper 6
         dw $9808        ; Item 11, Esper 7
         dw $9878        ; Item 12, Esper 8
         dw $A408        ; Item 13, Esper 9
         dw $A478        ; Item 14, Esper 10
         dw $B008        ; Item 15, Esper 11
         dw $B078        ; Item 16, Esper 12
         dw $BC08        ; Item 17, Esper 13
         dw $BC78        ; Item 18, Esper 14
         dw $C808        ; Item 19, Esper 15
         dw $C878        ; Item 20, Esper 16

; Cursor positions for Item menu options
C37D9D:  dw $1640        ; USE
         dw $1668        ; ARRANGE
         dw $16B0        ; RARE

; Cursor positions for Equip menu options
C38E64:  dw $1000        ; EQUIP
         dw $1038        ; OPTIMUM
         dw $1080        ; RMOVE
         dw $10B8        ; EMPTY

; Cursor positions for body parts
C38E80:  dw $2C00        ; R-Hand
         dw $3800        ; L-Hand
C38EE9:  dw $4400        ; Head, Relic 1
         dw $5000        ; Body, Relic 2

; Cursor positions for equipment list
C38EAB:  dw $6800        ; Item 1
         dw $7400        ; Item 2
         dw $8000        ; Item 3
         dw $8C00        ; Item 4
         dw $9800        ; Item 5
         dw $A400        ; Item 6
         dw $B000        ; Item 7
         dw $BC00        ; Item 8
         dw $C800        ; Item 9

; Cursor positions for Relic menu options
C38ED1:  dw $1010        ; Equip
         dw $1048        ; Remove

; Cursor positions for final battle lineup
C3AC68:  dw $2010        ; Reset
         dw $2C20        ; Actor 1
         dw $3820        ; Actor 2
         dw $4420        ; Actor 3
         dw $5020        ; Actor 4
         dw $5C20        ; Actor 5
         dw $6820        ; Actor 6
         dw $7420        ; Actor 7
         dw $8020        ; Actor 8
         dw $8C20        ; Actor 9
         dw $9820        ; Actor 10
         dw $A420        ; Actor 11
         dw $B020        ; Actor 12
         dw $BC10        ; End

; Cursor positions for Colosseum matchup menu
C3B337:  dw $B010        ; Member A
         dw $B048        ; Member B
         dw $B080        ; Member C
         dw $B0B8        ; Member D

; Cursor positions for main shop menu
C3B89A:  dw $3408        ; BUY
         dw $3430        ; SELL
         dw $3460        ; EXIT

; Cursor positions for Buy menu
C3B8B4:  dw $3400        ; Item 1
         dw $4000        ; Item 2
         dw $4C00        ; Item 3
         dw $5800        ; Item 4
         dw $6400        ; Item 5
         dw $7000        ; Item 6
         dw $7C00        ; Item 7
         dw $8800        ; Item 8

; ==============================[ Object data ]================================

; Animation table for main cursor
C3088D:  dw C30890       ; Sprite 1
         db $FE          ; Freeze

; OAM for hand cursor
C30890:  db $01          ; Tiles: 1
         dw $0080        ; 16x16, X+0, Y+0
         dw $3E00        ; Tile 0, pal 7, prio 3

; Animation table for blinking cursor
C30896:  dw C30890       ; Show cursor
         db $02          ; Frames: 2
         dw blkobj       ; Hide cursor
         db $02          ; Frames: 2
         dw C30000       ; Bogus...
         db $FF          ; Loop

; Pointers to scrollbar animation tables
C309DB:  dw C309EC       ; Flashing up
         dw C309F5       ; Flashing down
         dw C309E3       ; Flashing both ways

; Animation table for scrollbar flashing both ways
C309E3:  dw C309FE       ; Flash neither
         db $10          ; Frames: 16
         dw C30A07       ; Flash both
         db $10          ; Frames: 16
         dw C309FE       ; Bogus...
         db $FF          ; Loop

; Animation table for scrollbar pointing up
C309EC:  dw C309FE       ; Flash neither
         db $10          ; Frames: 16
         dw C30A10       ; Flash highest
         db $10          ; Frames: 16
         dw C309FE       ; Bogus...
         db $FF          ; Loop

; Animation table for scrollbar pointing down
C309F5:  dw C309FE       ; Flash neither
         db $10          ; Frames: 16
         dw C30A19       ; Flash lowest
         db $10          ; Frames: 16
         dw C309FE       ; Bogus...
         db $FF          ; Loop

; OAM for non-flashing scrollbar
C309FE:  db $02          ; Tiles: 2
         dw $0000,$3E02  ; ^: White
         dw $0800,$BE02  ; v: White

; OAM for scrollbar flashing both ways
C30A07:  db $02          ; Tiles: 2
         dw $0000,$3E12  ; ^: Black
         dw $0800,$BE12  ; v: Black

; OAM for scrollbar pointing up
C30A10:  db $02          ; Tiles: 2
         dw $0000,$3E12  ; ^: Black
         dw $0800,$BE02  ; v: White

; OAM for scrollbar pointing down
C30A19:  db $02          ; Tiles: 2
         dw $0000,$3E02  ; ^: White
         dw $0800,$BE12  ; v: Black

; Pointers for Config's large arrows
C339C7:  dw C339CB       ; Downward arrow
         dw C339D4       ; Upward arrow

; Animation table for large downward arrow
C339CB:  dw blkobj       ; Hide arrow
         db $10          ; Frames: 16
         dw C339DD       ; Show arrow
         db $10          ; Frames: 16
         dw blkobj       ; Bogus...
         db $FF          ; Loop

; Animation table for large upward arrow
C339D4:  dw blkobj       ; Hide arrow
         db $10          ; Frames: 16
         dw C3B461       ; Show arrow
         db $10          ; Frames: 16
         dw blkobj       ; Bogus...
         db $FF          ; Loop

; OAM for large downward arrow
C339DD:  db $01          ; Tiles: 1
         dw $B080        ; 16x16, X+0, Y+176
         dw $3E03        ; Tile 3, pal 7, prio 3

; OAM for small white arrow in actor naming menu
C3B448:  db $01          ; Tiles: 1
         dw $0009        ; 8x8, X+9, Y+0
         dw $3E02        ; Tile 2, pal 7, prio 3

; OAM for small black arrow in actor naming menu
C3B44D:  db $01          ; Tiles: 1
         dw $0009        ; 8x8, X+9, Y+0
         dw $3E12        ; Tile 18, pal 7, prio 3

; Animation table for small arrow in actor naming menu
C3B452:  dw C3B448       ; White arrow
         db $02          ; Frames: 2
         dw C3B44D       ; Black arrow
         db $02          ; Frames: 2
         dw C3B448       ; Bogus...
         db $FF          ; Loop

; OAM for large upward arrow
C3B461:  db $01          ; Tiles: 1
         dw $0080        ; 16x16, X+0, Y+0
         dw $BE03        ; Tile 3, pal 7, prio 3, V-Flip

; Pointers to portrait animation tables
C30AE9:  dw C30B8C       ; Portrait A
         dw C30BC4       ; Portrait B
         dw C30BFC       ; Portrait C
         dw C30C34       ; Portrait D

; Animation table for portrait A
C30B8C:  dw C30B8F       ; Show portrait
         db $FE          ; Freeze

; OAM for portrait A
C30B8F:  db $0D          ; Tiles: 13
         dw $0080,$2060  ; Tile 1
         dw $0090,$2062  ; Tile 2
         dw $1080,$2064  ; Tile 3
         dw $1090,$2066  ; Tile 4
         dw $0020,$2068  ; Tile 5
         dw $0820,$2069  ; Tile 6
         dw $1020,$206A  ; Tile 7
         dw $1820,$206B  ; Tile 8
         dw $2020,$206C  ; Tile 9
         dw $2000,$206D  ; Tile 10
         dw $2008,$206E  ; Tile 11
         dw $2010,$206F  ; Tile 12
         dw $2018,$2078  ; Tile 13

; Animation table for portrait B
C30BC4:  dw C30BC7       ; Show portrait
         db $FE          ; Freeze

; OAM for portrait B
C30BC7:  db $0D          ; Tiles: 13
         dw $0080,$2280  ; Tile 1
         dw $0090,$2282  ; Tile 2
         dw $1080,$2284  ; Tile 3
         dw $1090,$2286  ; Tile 4
         dw $0020,$2288  ; Tile 5
         dw $0820,$2289  ; Tile 6
         dw $1020,$228A  ; Tile 7
         dw $1820,$228B  ; Tile 8
         dw $2020,$228C  ; Tile 9
         dw $2000,$228D  ; Tile 10
         dw $2008,$228E  ; Tile 11
         dw $2010,$228F  ; Tile 12
         dw $2018,$2298  ; Tile 13

; Animation table for portrait C
C30BFC:  dw C30BFF       ; Show portrait
         db $FE          ; Freeze

; OAM for portrait C
C30BFF:  db $0D          ; Tiles: 13
         dw $0080,$24A0  ; Tile 1
         dw $0090,$24A2  ; Tile 2
         dw $1080,$24A4  ; Tile 3
         dw $1090,$24A6  ; Tile 4
         dw $0020,$24A8  ; Tile 5
         dw $0820,$24A9  ; Tile 6
         dw $1020,$24AA  ; Tile 7
         dw $1820,$24AB  ; Tile 8
         dw $2020,$24AC  ; Tile 9
         dw $2000,$24AD  ; Tile 10
         dw $2008,$24AE  ; Tile 11
         dw $2010,$24AF  ; Tile 12
         dw $2018,$24B8  ; Tile 13

; Animation table for portrait D
C30C34:  dw C30C37       ; Show portrait
         db $FE          ; Freeze

; OAM for portrait D
C30C37:  db $0D          ; Tiles: 13
         dw $0080,$26C0  ; Tile 1
         dw $0090,$26C2  ; Tile 2
         dw $1080,$26C4  ; Tile 3
         dw $1090,$26C6  ; Tile 4
         dw $0020,$26C8  ; Tile 5
         dw $0820,$26C9  ; Tile 6
         dw $1020,$26CA  ; Tile 7
         dw $1820,$26CB  ; Tile 8
         dw $2020,$26CC  ; Tile 9
         dw $2000,$26CD  ; Tile 10
         dw $2008,$26CE  ; Tile 11
         dw $2010,$26CF  ; Tile 12
         dw $2018,$26D8  ; Tile 13

; Base Y position for each portrait
face_y:  db $15          ; Portrait A
         db $45          ; Portrait B
         db $75          ; Portrait C
         db $A5          ; Portrait D

; X positions for actors on save files
C3197F:  dw $6850,$9880  ; File 1
         dw $6850,$9880  ; File 2
         dw $6850,$9880  ; File 3

; Y positions for actors on save files
C3198B:  dw $4040,$4040  ; File 1
         dw $7878,$7878  ; File 2
         dw $B0B0,$B0B0  ; File 3

; Actors' OAM complements when on "New Game"
C31997:  dw $0202,$0202  ; File 1: Grayscale
         dw $0202,$0202  ; File 2: Grayscale
         dw $0202,$0202  ; File 3: Grayscale

; Actors' OAM complements when on save file 1
C319A3:  dw $0000,$0000  ; File 1: None
         dw $0202,$0202  ; File 2: Grayscale
         dw $0202,$0202  ; File 3: Grayscale

; Actors' OAM complements when on save file 2
C319AF:  dw $0202,$0202  ; File 1: Grayscale
         dw $0000,$0000  ; File 2: None
         dw $0202,$0202  ; File 3: Grayscale

; Actors' OAM complements when on save file 3
C319BB:  dw $0202,$0202  ; File 1: Grayscale
         dw $0202,$0202  ; File 2: Grayscale
         dw $0000,$0000  ; File 3: None

; Relative X positions for party members in Lineup menu
C378E3:  db $18          ; NW
         db $18          ; SW
         db $38          ; NE
         db $38          ; SE

; Y positions for party members in Lineup menu
C378E7:  db $9C          ; NW
         db $B8          ; SW
         db $9C          ; NE
         db $B8          ; SE

; Party sprite positions for Colosseum matchup
colobj:  dw $A820        ; Member A
         dw $A858        ; Member B
         dw $A890        ; Member C
         dw $A8C8        ; Member D

; Moogle object data for Umaro's ending scene
moogle:  db $3A          ; A: X = 58
         dw $F47F        ; A: Bump anim ptr
         dw $01A4        ; A: Walk timer
         db $2E          ; B: X = 46
         dw $F485        ; B: Bump anim ptr
         dw $01B8        ; B: Walk timer
         db $21          ; C: X = 33
         dw $F48B        ; C: Bump anim ptr
         dw $01CC        ; C: Walk timer
         db $14          ; D: X = 20
         dw $F491        ; D: Bump anim ptr
         dw $01E0        ; D: Walk timer

; =============================[ Window layouts ]==============================

; Save and Load menus
C3189C:  dw $58CB,$021C  ; 30x4 at $58CB (Top window)
C318A0:  dw $59CB,$051C  ; 30x7 at $59CB (File A)
C318A4:  dw $5B8B,$051C  ; 30x7 at $5B8B (File B)
C318A8:  dw $5D4B,$051C  ; 30x7 at $5D4B (File C)

; Main menu
C331BB:  dw $58B7,$0D06  ; 08x15 at $58B7 (Options)
C331CF:  dw $60B9,$0207  ; 09x04 at $60B9 (Order; L part)
         dw $5885,$0201  ; 03x04 at $5885 (Order; R part)
         db $00          ; End

; Main menu and save file confirmation (and others)
C331C7:  dw $588B,$181C  ; 30x26 at $588B (Party)
         dw $5C77,$0206  ; 08x04 at $5C77 (Time)
         dw $5D75,$0507  ; 09x07 at $5D75 (Steps)
         db $00          ; End

; Save file confirmation
C331CB:  dw $58B5,$0A07  ; 09x12 at $58B5 (Yes/No)

; Config menu
C339E2:  dw $590B,$161C  ; 30x24 at $590B (Main)
         dw $58B7,$0206  ; 08x04 at $58B7 (Title)
         db $00          ; End

; Cmd.Set menu
C34490:  dw $591F,$0412  ; 20x06 at $591F (Command set A)
         dw $5A9F,$0412  ; 20x06 at $5A9F (Command set B)
         dw $5C1F,$0412  ; 20x06 at $5C1F (Command set C)
         dw $5D9F,$0412  ; 20x06 at $5D9F (Command set D)
         dw $590B,$0408  ; 10x06 at $590B (Member name A)
         dw $5A8B,$0408  ; 10x06 at $5A8B (Member name B)
         dw $5C0B,$0408  ; 10x06 at $5C0B (Member name C)
         dw $5D8B,$0408  ; 10x06 at $5D8B (Member name D)
         dw $588B,$0108  ; 10x03 at $588B (Arrange)
blkobj:  db $00          ; End

; Controller menu
C348F3:  dw $5B0B,$0B1C  ; 30x13 at $5B0B

; Skills menu (up to C34DA8) and submenus (C34DAC+)
C34D98:  dw $59CB,$051C  ; 30x07 at $59CB (Stats)
         dw $588B,$031C  ; 30x05 at $588B (Top)
         dw $5B8B,$0C1C  ; 30x14 at $5B8B (Bottom)
         dw $588B,$0407  ; 09x06 at $588B (Upper submenus)
         dw $5A0B,$0A07  ; 09x12 at $5A0B (Lower submenus)
         dw $61CB,$051C  ; 30x07 at $61CB (Stats)
         dw $638B,$0C1C  ; 30x14 at $638B (List)
         dw $608B,$031C  ; 30x05 at $608B (Description)
         dw $6175,$0107  ; 09x03 at $6175 (Title)
         db $00          ; End

; Status menu
C35F79:  dw $588B,$0106  ; 08x03 at $588B (Title)
         dw $5AEB,$0609  ; 11x08 at $5AEB (Commands)
         db $00          ; End

; Panel in Gogo's Status menu
C35F85:  dw $58C7,$1200  ; 02x20 at $58C7 (Left side)
         dw $6087,$1207  ; 09x20 at $6087 (Right side)

; Actor naming menu
C36761:  dw $5A57,$1116  ; 24x19 at $5A57 (Options)
         dw $599B,$0212  ; 20x04 at $599B (Name)
         dw $588B,$0505  ; 07x07 at $588B (Portrait)
         dw $5899,$0214  ; 22x04 at $5899 ("Please...")
         db $00          ; End

; Lineup menu
C37598:  dw $588B,$0206  ; 08x04 at $588B (Title)
         dw $5CCB,$071C  ; 30x09 at $5CCB (Parties)
         dw $598B,$051C  ; 30x07 at $598B (Stats)
         dw $5B0B,$061C  ; 30x08 at $5B0B (Candidates)
         dw $589B,$0214  ; 22x04 at $589B (Messages)
         dw $5D0B,$0608  ; 10x08 at $5D0B (Party 1)
         db $00          ; End

; Party boxes for Lineup menu
C375B0:  dw $5D33,$0608  ; 10x08 at $5D33 (Party 3)
C375B4:  dw $5D1F,$0608  ; 10x08 at $5D1F (Party 2)
         db $00          ; End

; Item menu and gear data menu
C37E13:  dw $60AD,$180D  ; 15x26 at $60AD (Gear stats; L part)
         dw $5885,$1801  ; 03x26 at $5885 (Gear stats; R part)
         dw $5ACB,$0F1C  ; 30x17 at $5ACB (Main)
         dw $598B,$031C  ; 30x05 at $598B (Description)
         dw $588B,$0204  ; 06x04 at $588B (Title)
         dw $5897,$0216  ; 24x04 at $5897 (Options)
         db $00          ; End

; Spell or item usage menu
C3583F:  dw $589D,$1813  ; 21x26 at $589D (Party)
         dw $588B,$0207  ; 09x04 at $588B (Spell)
         dw $598B,$0307  ; 09x05 at $598B (MP cost or quantity)
         db $00          ; End

; Item usage menu
C38A3F:  dw $588B,$020D  ; 15x04 at $588B (Item)

; Equip and Relic menus
C3947F:  dw $5B4B,$0D1C  ; 30x15 at $5B4B (Stats w/o title)
         dw $590B,$071C  ; 30x09 at $590B (Gear w/o title)
         dw $588B,$021C  ; 30x04 at $588B (Options)
         dw $634B,$0D1C  ; 30x15 at $634B (Stats w/ title)
         dw $610B,$071C  ; 30x09 at $610B (Gear w/ title)
         dw $60B7,$0206  ; 08x04 at $60B7 (Title)
         db $00          ; End

; Colosseum item menu
C3AD8A:  dw $588B,$0209  ; 11x04 at $588B (Title)
         dw $58A1,$0211  ; 19x04 at $58A1 ("Select...")
         dw $598B,$031C  ; 30x05 at $598B (Description)
         dw $5ACB,$0F1C  ; 30x17 at $5ACB (Items)
         db $00          ; End

; Colosseum matchup menu
C3B33F:  dw $588B,$020D  ; 15x4 at $588B (Prize)
         dw $58A9,$020D  ; 15x4 at $58A9 (Bet)
         dw $5CCB,$071C  ; 30x9 at $5CCB (Party)
         db $00          ; End

; Sell menu and main shop menu
C3BC1B:  dw $588B,$0207  ; 09x04 at $588B (Title)
         dw $589D,$0213  ; 21x04 at $589D (Dialogue)
         dw $598B,$0210  ; 18x04 at $598B (Options)
         dw $59AF,$020A  ; 12x04 at $59AF (GP)
         dw $5A8B,$101C  ; 30x18 at $5A8B (Items)

; Buy menu
         dw $608B,$0207  ; 09x04 at $608B (Title)
         dw $609D,$0213  ; 21x04 at $609D (Dialogue)
         dw $618B,$0C1C  ; 30x14 at $618B (Items)
         dw $650B,$061C  ; 30x08 at $650B (Actors)

; Shop order menus
         dw $688B,$0207  ; 09x04 at $688B (Title)
         dw $689D,$0213  ; 21x04 at $689D (Dialogue)
         dw $698B,$0711  ; 19x09 at $698B (Item)
         dw $6BCB,$031C  ; 30x05 at $6BCB (Description)
         dw $6D0B,$061C  ; 30x08 at $6D0B (Actors)
         dw $69B1,$0709  ; 11x09 at $69B1 (Amounts)
         db $00          ; End

; Pointers to window layouts for save files
sv_win:  dw C318A0       ; File A
         dw C318A4       ; File B
         dw C318A8       ; File C

; ==========================[ Text shifting tables ]===========================

; Text shifting table for main menu
C336C2:  db $0F,$00,$00  ; Nothing
         db $0F,$03,$00  ; Item
         db $0F,$04,$00  ; Skills
         db $0F,$05,$00  ; Equip
         db $0F,$06,$00  ; Relic
         db $0F,$07,$00  ; Status
         db $0F,$08,$00  ; Config
         db $0F,$09,$00  ; Save
         db $07,$08,$00  ; Nothing
         db $28,$00,$00  ; Time, etc.
         db $00          ; End

; Text shifting table for Status menu
C3622A:  db $27,$00,$00  ; Status
         db $0C,$04,$00  ; Cmd choice A
         db $0C,$08,$00  ; Cmd choice B
         db $0C,$0C,$00  ; Cmd choice C
         db $0C,$10,$00  ; Cmd choice D
         db $0C,$14,$00  ; Command A
         db $0C,$18,$00  ; Command B
         db $0C,$1C,$00  ; Command C
         db $0C,$20,$00  ; Command D
         db $0C,$24,$00  ; Needed exp.
         db $0C,$28,$00  ; Bat.Pwr
         db $0C,$2C,$00  ; Defense
         db $0C,$30,$00  ; Evade
         db $0C,$34,$00  ; Mag.Def
         db $0C,$38,$00  ; MBlock
         db $0C,$3C,$00  ; Nothing
         db $00          ; End

; BG1 H-Shift table for actor naming menu (merges tilemaps)
C36889:  db $47,$00,$01  ; Title, name
         db $50,$00,$00  ; Upper chars
         db $50,$00,$00  ; Lower chars
         db $10,$00,$01  ; Blank area
         db $00          ; End

; BG1 V-Shift table for actor naming menu (condenses text)
C36896:  db $47,$00,$00  ; Title, name
         db $50,$D0,$FF  ; Upper chars
         db $50,$D0,$FF  ; Lower chars
         db $10,$00,$00  ; Blank area
         db $00          ; End

; Text shifting table for Lineup menu
C375E4:  db $20,$02,$00  ; Title
         db $08,$00,$00  ; Nothing
         db $0B,$04,$00  ; Nothing
         db $0C,$08,$00  ; Level
         db $0C,$0C,$00  ; HP
         db $0C,$10,$00  ; MP
         db $00          ; End

; BG1 H-Shift table for inventory-style menus
C37EDA:  db $28,$00,$01  ; Nothing
         db $2F,$00,$01  ; Nothing
         db $78,$00,$00  ; Items
         db $1E,$00,$01  ; Nothing
         db $00          ; End

; BG1 V-Shift table for inventory-style menus
C37EE7:  db $27,$80,$00  ; Nothing?
         db $08,$00,$00  ; Nothing?
         db $0C,$04,$00  ; Nothing?
         db $0C,$08,$00  ; Nothing?
         db $08,$80,$00  ; Nothing?
         db $08,$00,$00  ; Nothing?
C37EF9:  db $04,$AC,$FF  ; Item row 1
         db $04,$AC,$FF  ; Item row 2
         db $04,$AC,$FF  ; Item row 3
         db $04,$B0,$FF  ; Item row 4
         db $04,$B0,$FF  ; Item row 5
         db $04,$B0,$FF  ; Item row 6
         db $04,$B4,$FF  ; Item row 7
         db $04,$B4,$FF  ; Item row 8
         db $04,$B4,$FF  ; Item row 9
         db $04,$B8,$FF  ; Item row 10
         db $04,$B8,$FF  ; Item row 11
         db $04,$B8,$FF  ; Item row 12
         db $04,$BC,$FF  ; Item row 13
         db $04,$BC,$FF  ; Item row 14
         db $04,$BC,$FF  ; Item row 15
         db $04,$C0,$FF  ; Item row 16
         db $04,$C0,$FF  ; Item row 17
         db $04,$C0,$FF  ; Item row 18
         db $04,$C4,$FF  ; Item row 19
         db $04,$C4,$FF  ; Item row 20
         db $04,$C4,$FF  ; Item row 21
         db $04,$C8,$FF  ; Item row 22
         db $04,$C8,$FF  ; Item row 23
         db $04,$C8,$FF  ; Item row 24
         db $04,$CC,$FF  ; Item row 25
         db $04,$CC,$FF  ; Item row 26
         db $04,$CC,$FF  ; Item row 27
         db $04,$D0,$FF  ; Item row 28
         db $04,$D0,$FF  ; Item row 29
         db $04,$D0,$FF  ; Item row 30
         db $1E,$00,$00  ; Nothing
         db $00          ; End

; BG3 V-Shift table for Item and Colosseum menus
C37F57:  db $2F,$00,$00  ; Title
         db $0C,$04,$00  ; Desc row 1
         db $0C,$08,$00  ; Desc row 2
         db $0C,$0C,$00  ; 2-hand
         db $0C,$10,$00  ; 50% Dmg
         db $0C,$14,$00  ; Vigor
         db $0C,$18,$00  ; Speed
         db $0C,$1C,$00  ; Stamina
         db $0C,$20,$00  ; Mag.Pwr
         db $0C,$24,$00  ; Bat.Pwr
         db $0C,$28,$00  ; Defense
         db $0C,$2C,$00  ; Evade
         db $0C,$30,$00  ; Mag.Def
         db $0C,$34,$00  ; MBlock
         db $0C,$38,$00  ; Nothing
         db $0C,$3C,$00  ; Nothing
         db $00          ; End

; BG1 H-Shift table for gear list browsing
C39564:  db $27,$00,$01  ; Nothing?
         db $3C,$00,$01  ; Nothing?
         db $6C,$00,$00  ; Item list
         db $1E,$00,$01  ; Nothing?
         db $00          ; End

; BG1 V-Shift table for gear list browsing
C39571:  db $3F,$00,$00  ; Nothing?
         db $0C,$04,$00  ; Nothing?
         db $0C,$08,$00  ; Nothing?
         db $0A,$0C,$00  ; Nothing?
         db $01,$0C,$00  ; Nothing?
         db $01,$0C,$00  ; Nothing?
C39583:  db $04,$A0,$FF  ; Item row 1
         db $04,$A0,$FF  ; Item row 2
         db $04,$A0,$FF  ; Item row 3
         db $04,$A4,$FF  ; Item row 4
         db $04,$A4,$FF  ; Item row 5
         db $04,$A4,$FF  ; Item row 6
         db $04,$A8,$FF  ; Item row 7
         db $04,$A8,$FF  ; Item row 8
         db $04,$A8,$FF  ; Item row 9
         db $04,$AC,$FF  ; Item row 10
         db $04,$AC,$FF  ; Item row 11
         db $04,$AC,$FF  ; Item row 12
         db $04,$B0,$FF  ; Item row 13
         db $04,$B0,$FF  ; Item row 14
         db $04,$B0,$FF  ; Item row 15
         db $04,$B4,$FF  ; Item row 16
         db $04,$B4,$FF  ; Item row 17
         db $04,$B4,$FF  ; Item row 18
         db $04,$B8,$FF  ; Item row 19
         db $04,$B8,$FF  ; Item row 20
         db $04,$B8,$FF  ; Item row 21
         db $04,$BC,$FF  ; Item row 22
         db $04,$BC,$FF  ; Item row 23
         db $04,$BC,$FF  ; Item row 24
         db $04,$C0,$FF  ; Item row 25
         db $04,$C0,$FF  ; Item row 26
         db $04,$C0,$FF  ; Item row 27
         db $1E,$10,$00  ; Nothing?
         db $00          ; End

; V-Shift table for gear menus and final battle lineup
C395D8:  db $0F,$00,$00  ; Nothing
         db $0C,$04,$00  ; Options, title
         db $0C,$08,$00  ; Reequip mode
         db $0C,$0C,$00  ; R-Hand
         db $0C,$10,$00  ; L-Hand
         db $0C,$14,$00  ; Head, Relic 1
         db $0C,$18,$00  ; Body, Relic 2
         db $0C,$1C,$00  ; Member 2's armor
         db $0C,$20,$00  ; Vigor
         db $0C,$24,$00  ; Speed
         db $0C,$28,$00  ; Stamina
         db $0C,$2C,$00  ; Mag.Pwr
         db $0C,$30,$00  ; Bat.Pwr
         db $0C,$34,$00  ; Defense
         db $0C,$38,$00  ; Evade
         db $0C,$3C,$00  ; Mag.Def
         db $0C,$40,$00  ; MBlock
         db $00          ; End

; BG3 text shifting table for shop
C3C037:  db $3B,$04,$00  ; Title, Item 1
         db $0C,$08,$00  ; Item 2
         db $0C,$0C,$00  ; Item 3
         db $0C,$10,$00  ; Item 4
         db $0C,$14,$00  ; Item 5
         db $0C,$18,$00  ; Item 6
         db $0C,$1C,$00  ; Item 7
         db $0C,$20,$00  ; Item 8
         db $0C,$24,$00  ; Nothing
         db $0C,$28,$00  ; Nothing
         db $0C,$2C,$00  ; Nothing
         db $0C,$30,$00  ; Nothing
         db $0C,$34,$00  ; Nothing
         db $0C,$38,$00  ; Nothing
         db $0C,$3C,$00  ; Nothing
         db $00          ; End

; Text shifting table for save file confirmation, Colosseum matchup
C318F0:  db $20,$00,$00  ; 32 lines, shift by 0 px
         db $0C,$04,$00  ; 12 lines, shift by 4 px
         db $0C,$08,$00  ; 12 lines, shift by 8 px
         db $08,$C8,$FF  ; 8 lines, shift by -38 px
         db $50,$00,$00  ; 80 lines, shift by 0 px
         db $58,$00,$00  ; 88 lines, shift by 0 px
         db $00          ; End

; Text shifting table for Cmd.Set menu
C3468E:  db $1F,$00,$00  ; Arrange
         db $0C,$04,$00  ; Row 1
         db $0C,$08,$00  ; Row 2
         db $24,$0C,$00  ; Row 3, row 4
         db $0C,$10,$00  ; Row 5
         db $24,$14,$00  ; Row 6, row 7
         db $0C,$18,$00  ; Row 8
         db $24,$1C,$00  ; Row 9, row 10
         db $0C,$20,$00  ; Row 11
         db $24,$24,$00  ; Row 12
         db $00          ; End

; BG1 H-Shift table for skill menus (merges tilemaps)
C34E72:  db $27,$00,$01  ; Nothing
         db $48,$00,$01  ; Member stats
         db $60,$00,$00  ; Spells, etc.
         db $1E,$00,$01  ; Nothing
         db $00          ; End

; BG1 V-Shift table for skill menus (condenses text)
C34E7F:  db $3F,$00,$00  ; LV
         db $0C,$04,$00  ; HP
         db $0C,$08,$00  ; MP
         db $0A,$0C,$00  ; Nothing
         db $01,$0C,$00  ; Nothing
         db $0D,$08,$00  ; Nothing
C34E91:  db $04,$94,$FF  ; Ability row A
         db $04,$94,$FF  ; Ability row B
         db $04,$94,$FF  ; Ability row C
         db $04,$98,$FF  ; Ability row D
         db $04,$98,$FF  ; Ability row E
         db $04,$98,$FF  ; Ability row F
         db $04,$9C,$FF  ; Ability row G
         db $04,$9C,$FF  ; Ability row H
         db $04,$9C,$FF  ; Ability row I
         db $04,$A0,$FF  ; Ability row J
         db $04,$A0,$FF  ; Ability row K
         db $04,$A0,$FF  ; Ability row L
         db $04,$A4,$FF  ; Ability row M
         db $04,$A4,$FF  ; Ability row N
         db $04,$A4,$FF  ; Ability row O
         db $04,$A8,$FF  ; Ability row P
         db $04,$A8,$FF  ; Ability row Q
         db $04,$A8,$FF  ; Ability row R
         db $04,$AC,$FF  ; Ability row S
         db $04,$AC,$FF  ; Ability row T
         db $04,$AC,$FF  ; Ability row U
         db $04,$B0,$FF  ; Ability row V
         db $04,$B0,$FF  ; Ability row W
         db $04,$B0,$FF  ; Ability row X
         db $1E,$20,$FF  ; Ability row Y
         db $00          ; End

; BG3 vertical shift table for skill menus
C34E26:  db $4F,$02,$00  ; SwdTech and above
         db $40,$02,$00  ; Blitz and below
         db $00          ; End

; Shift amount for list scrolling down, every 3rd of 3 frames
C3815F:  dw $0008        ; Row A: +8 px down
         dw $0004        ; Row B: +4 px down
         dw $0004        ; Row C: +4 px down

; Shift amount for list scrolling down, every 2nd of 3 frames
         dw $0004        ; Row A: +4 px down
         dw $0008        ; Row B: +8 px down
         dw $0004        ; Row C: +4 px down

; Shift amount for list scrolling down, every 1st of 3 frames
         dw $0004        ; Row A: +4 px down
         dw $0004        ; Row B: +4 px down
         dw $0008        ; Row C: +8 px down

; Shift amount for list scrolling up, every 3rd of 3 frames
         dw $FFFC        ; Row A: +4 px up
         dw $FFFC        ; Row B: +4 px up
         dw $FFF8        ; Row C: +8 px up

; Shift amount for list scrolling up, every 2nd of 3 frames
         dw $FFFC        ; Row A: +4 px up
         dw $FFF8        ; Row B: +8 px up
         dw $FFFC        ; Row C: +4 px up

; Shift amount for list scrolling up, every 1st of 3 frames
         dw $FFF8        ; Row A: +8 px up
         dw $FFFC        ; Row B: +4 px up
         dw $FFFC        ; Row C: +4 px up

; ============================[ Tilemap pointers ]=============================

; Tilemap pointers for stats of member 1
C3332D:  dw $39A7        ; Level
         dw $39E3        ; HP
         dw $39ED        ; Max HP
         dw $3A23        ; MP
         dw $3A2D        ; Max MP

; Tilemap pointers for stats of member 2
C33379:  dw $3B27        ; Level
         dw $3B63        ; HP
         dw $3B6D        ; Max HP
         dw $3BA3        ; MP
         dw $3BAD        ; Max MP

; Tilemap pointers for stats of member 3
C333C5:  dw $3CA7        ; Level
         dw $3CE3        ; HP
         dw $3CED        ; Max HP
         dw $3D23        ; MP
         dw $3D2D        ; Max MP

; Tilemap pointers for stats of member 4
C33411:  dw $3E27        ; Level
         dw $3E63        ; HP
         dw $3E6D        ; Max HP
         dw $3EA3        ; MP
         dw $3EAD        ; Max MP

; Tilemap pointers for Mag.Order strings
C340CC:  dw $4329        ; Spell type C
         dw $42A9        ; Spell type B
         dw $4229        ; Spell type A

; Actor name positions for Cmd.Set menu
actpos:  dw $798F        ; Member A
         dw $7B4F        ; Member B
         dw $7D0F        ; Member C
         dw $7ECF        ; Member D

; Command positions for Cmd.Set menu
cmdpos:  dw $7AAD        ; Member A, command 4
         dw $7A37        ; Member A, command 3
         dw $7A23        ; Member A, command 2
         dw $79AD        ; Member A, command 1
         dw $7C6D        ; Member B, command 4
         dw $7BF7        ; Member B, command 3
         dw $7BE3        ; Member B, command 2
         dw $7B6D        ; Member B, command 1
         dw $7E2D        ; Member C, command 4
         dw $7DB7        ; Member C, command 3
         dw $7DA3        ; Member C, command 2
         dw $7D2D        ; Member C, command 1
         dw $7FED        ; Member D, command 4
         dw $7F77        ; Member D, command 3
         dw $7F63        ; Member D, command 2
         dw $7EED        ; Member D, command 1

; Actor name positions for Controller menu
cnt_a:   dw $7BCF        ; Member A
         dw $7C4F        ; Member B
         dw $7CCF        ; Member C
         dw $7D4F        ; Member D

; Text positions for gear data menu
C38653:  dw $7E0F        ; Actor name 1
         dw $7E23        ; Actor name 2
         dw $7E37        ; Actor name 3
         dw $7E8F        ; Actor name 4
         dw $7EA3        ; Actor name 5
         dw $7EB7        ; Actor name 6
         dw $7F0F        ; Actor name 7
         dw $7F23        ; Actor name 8
         dw $7F37        ; Actor name 9
         dw $7F8F        ; Actor name 10
         dw $7FA3        ; Actor name 11
         dw $7FB7        ; Actor name 12
         dw $800F        ; Actor name 13
         dw $8023        ; Actor name 14
;        dw $8037        ; Actor name 15 (unused)

; Tilemap pointers for stats in Skills menu
C34F12:  dw $4237        ; Level
         dw $42B3        ; HP
         dw $42BD        ; Max HP
         dw $4333        ; MP
         dw $433D        ; Max MP

; Tilemap pointers for stats in Status menu
C36096:  dw $3A27        ; Level
         dw $3A63        ; HP
         dw $3A6D        ; Max HP
         dw $3AA3        ; MP
         dw $3AAD        ; Max MP
C3609_:  dw $8861        ; Mag.Pwr
         dw $7FE1        ; Stamina
         dw $7F61        ; Speed
         dw $7EE1        ; Vigor
         dw $7F7D        ; Evade
         dw $887D        ; MBlock

; Tilemap pointers for stats in Lineup menu
C379E6:  dw $3A77        ; LV
         dw $3AF3        ; HP
         dw $3AFD        ; Max HP
         dw $3B73        ; MP
         dw $3B7D        ; Max MP

; Tilemap pointers for stat modifiers
modpos:  dw $85C5        ; Mag.Pwr
         dw $8545        ; Stamina
         dw $84C5        ; Speed
         dw $8445        ; Vigor

; Actor name positions for party gear overview
gearchr: dw $390D        ; Member A
         dw $3B0D        ; Member B
         dw $3D0D        ; Member C
         dw $3F0D        ; Member D

; Base Y positions for party's gear
gear_y:  db $04          ; Member A
         db $0C          ; Member B
         db $14          ; Member C
         db $1C          ; Member D

; X positions for party gear overview
C38FD5:  db $11          ; Relic 2
         db $02          ; Relic 1
         db $11          ; Body
         db $02          ; Head
         db $11          ; L-Hand
         db $02          ; R-Hand

; Relative Y positions for party gear overview
C38FDB:  db $05          ; Relic 2
         db $05          ; Relic 1
         db $03          ; Body
         db $03          ; Head
         db $01          ; L-Hand
         db $01          ; R-Hand

; Positions for current stats in gear menus
C3913_:  dw $7E37        ; Mag.Pwr
         dw $7DB7        ; Stamina
         dw $7D37        ; Speed
         dw $7CB7        ; Vigor
         dw $7FB7        ; Evade
         dw $80B7        ; MBlock

; Positions for projected stats in gear menus
projsts: dw $7E3F        ; Mag.Pwr
         dw $7DBF        ; Stamina
         dw $7D3F        ; Speed
         dw $7CBF        ; Vigor
         dw $7FBF        ; Evade
         dw $80BF        ; MBlock

; Party name positions for Colosseum matchup
colnm:   dw $7E4F        ; Member A
         dw $7E5D        ; Member B
         dw $7E6B        ; Member C
         dw $7E79        ; Member D

; ==================================[ Text ]===================================

; Text pointers and positions for save files
ldr_tbl: dw C31A3C       ; A: "Time"
         dw C31A43       ; B: "Time"
         dw C31A4A       ; C: "Time"
         dw C31A5D       ; A: "LV"
         dw C31A62       ; B: "LV"
         dw C31A67       ; C: "LV"
         dw $3A4F        ; A: Actor name
         dw $3C0F        ; B: Actor name
         dw $3DCF        ; C: Actor name
         dw $7AFD        ; A: Max HP
         dw $7CBD        ; B: Max HP
         dw $7E7D        ; C: Max HP
         dw $7AF3        ; A: HP
         dw $7CB3        ; B: HP
         dw $7E73        ; C: HP
         dw $7A81        ; A: Level
         dw $7C41        ; B: Level
         dw $7E01        ; C: Level
         dw C31A6C       ; A: "/"
         dw C31A70       ; B: "/"
         dw C31A74       ; C: "/"
         dw $7B13        ; A: Minutes
         dw $7CD3        ; B: Minutes
         dw $7E93        ; C: Minutes
         dw $7B0D        ; A: Hours
         dw $7CCD        ; B: Hours
         dw $7E8D        ; C: Hours
         dw C31A51       ; A: ":"
         dw C31A55       ; B: ":"
         dw C31A59       ; C: ":"

; Pointers to "Empty" for save files
emptysv: dw C31A24       ; File A
         dw C31A2C       ; File B
         dw C31A34       ; File C

; Positioned text for save file menus
C31A24:  dw $7A4F : db "Empty",$00
C31A2C:  dw $7C0F : db "Empty",$00
C31A34:  dw $7DCF : db "Empty",$00
C31A3C:  dw $7ACF : db "Time",$00
C31A43:  dw $7C8F : db "Time",$00
C31A4A:  dw $7E4F : db "Time",$00
C31A51:  dw $7B11 : db ":",$00
C31A55:  dw $7CD1 : db ":",$00
C31A59:  dw $7E91 : db ":",$00
C31A5D:  dw $7A7B : db "LV",$00
C31A62:  dw $7C3B : db "LV",$00
C31A67:  dw $7DFB : db "LV",$00
C31A6C:  dw $7AFB : db "/",$00
C31A70:  dw $7CBB : db "/",$00
C31A74:  dw $7E7B : db "/",$00
C31A78:  dw $7967 : db "Save",$00
C31A7F:  dw $7963 : db "New Game",$00

; Ordered list of item icons for Arrange command
C326F5:  db $FF          ; No icon
         db $D8          ; Dirk
         db $D9          ; Sword
         db $DA          ; Lance
         db $DB          ; Knife
         db $DC          ; Rod
         db $DD          ; Brush
         db $DE          ; Stars
         db $DF          ; Special
         db $E0          ; Gambler
         db $E1          ; Claw
         db $E2          ; Shield
         db $E3          ; Helmet
         db $E4          ; Armor
         db $E5          ; Tool
         db $E6          ; Skean
         db $E7          ; Relic

; Text pointers and positions for party data
ptytbl:  dw C3332D       ; A: Stat pos tbl
         dw C33379       ; B: Stat pos tbl
         dw C333C5       ; C: Stat pos tbl
         dw C33411       ; D: Stat pos tbl
         dw $3919        ; A: Name pos
         dw $3A99        ; B: Name pos
         dw $3C19        ; C: Name pos
         dw $3D99        ; D: Name pos
         dw C33731       ; A: Slash data ptr
         dw C33735       ; B: Slash data ptr
         dw C33739       ; C: Slash data ptr
         dw C3373D       ; D: Slash data ptr
         dw $3927        ; A: Class/KO pos
         dw $3AA7        ; B: Class/KO pos
         dw $3C27        ; C: Class/KO pos
         dw $3DA7        ; D: Class/KO pos
         dw $1578        ; A: Icon pos
         dw $4578        ; B: Icon pos
         dw $7578        ; C: Icon pos
         dw $A578        ; D: Icon pos
         dw C33741       ; A: Blue text ptr
         dw C33747       ; B: Blue text ptr
         dw C3374D       ; C: Blue text ptr
         dw C33753       ; D: Blue text ptr

; Text for Wound status
C3371B:  db "Wounded"

; Text pointers for main menu and save file submenus
C33723:  dw C33804       ; Time
         dw C3380F       ; Steps
         dw C33850       ; Order
         dw C33817       ; Gp
C33729:  dw C337CB       ; Item
         dw C337D2       ; Skills
         dw C337E3       ; Relic
         dw C337EB       ; Status
         dw C337DB       ; Equip
         dw C337F4       ; Config
C33731:  dw C3377E       ; /
         dw C33782       ; /
C33735:  dw C33795       ; /
         dw C33799       ; /
C33739:  dw C337AC       ; /
         dw C337B0       ; /
C3373D:  dw C337C3       ; /
         dw C337C7       ; /
C33741:  dw C3376F       ; LV
         dw C33774       ; HP
         dw C33779       ; MP
C33747:  dw C33786       ; LV
         dw C3378B       ; HP
         dw C33790       ; MP
C3374D:  dw C3379D       ; LV
         dw C337A2       ; HP
         dw C337A7       ; MP
C33753:  dw C337B4       ; LV
         dw C337B9       ; HP
         dw C337BE       ; MP
C3375D:  dw C3381C       ; Yes
         dw C33822       ; No
         dw C33827       ; This
         dw C3382E       ; data?
C33765:  dw C3381C       ; Yes
         dw C33822       ; No
         dw C33836       ; Erasing
         dw C33840       ; data.
         dw C33848       ; Okay?

; Positioned text for main menu and save file submenus
C3376F:  dw $399D : db "LV",$00
C33774:  dw $39DD : db "HP",$00
C33779:  dw $3A1D : db "MP",$00
C3377E:  dw $39EB : db "/",$00
C33782:  dw $3A2B : db "/",$00
C33786:  dw $3B1D : db "LV",$00
C3378B:  dw $3B5D : db "HP",$00
C33790:  dw $3B9D : db "MP",$00
C33795:  dw $3B6B : db "/",$00
C33799:  dw $3BAB : db "/",$00
C3379D:  dw $3C9D : db "LV",$00
C337A2:  dw $3CDD : db "HP",$00
C337A7:  dw $3D1D : db "MP",$00
C337AC:  dw $3CEB : db "/",$00
C337B0:  dw $3D2B : db "/",$00
C337B4:  dw $3E1D : db "LV",$00
C337B9:  dw $3E5D : db "HP",$00
C337BE:  dw $3E9D : db "MP",$00
C337C3:  dw $3E6B : db "/",$00
C337C7:  dw $3EAB : db "/",$00
C337CB:  dw $7939 : db "Item",$00
C337D2:  dw $79B9 : db "Skills",$00
C337DB:  dw $7A39 : db "Equip",$00
C337E3:  dw $7AB9 : db "Relic",$00
C337EB:  dw $7B39 : db "Status",$00
C337F4:  dw $7BB9 : db "Config",$00
C337FD:  dw $7C39 : db "Save",$00
C33804:  dw $7CBB : db "Time",$00
C3380B:  dw $7CFF : db ":",$00
C3380F:  dw $7DB7 : db "Steps",$00
C33817:  dw $7E77 : db "Gp",$00
C3381C:  dw $7ABD : db "Yes",$00
C33822:  dw $7B3D : db "No",$00
C33827:  dw $7937 : db "This",$00
C3382E:  dw $79B7 : db "data?",$00
C33836:  dw $7937 : db "Erasing",$00
C33840:  dw $79B7 : db "data.",$00
C33848:  dw $7A37 : db "Okay?",$00
C33850:  dw $813D : db "Order",$00

; Text pointers for Config menu
C34993:  dw C349AA       ; Bat.Mode
         dw C349B5       ; Bat.Speed
         dw C349C1       ; Msg.Speed
         dw C349CD       ; Cmd.Set
         dw C349D7       ; Gauge
         dw C349DF       ; Sound
         dw C3498A       ; Cursor
         dw C349E7       ; Reequip
         dw C3490B       ; Controller
         dw C34A34       ; Mag.Order
         dw C34A40       ; Window
         dw C34A49       ; Color
C34907:  dw C3491F       ; Fast
         dw C34926       ; Slow
         dw C34A87       ; R
         dw C34A8B       ; G
         dw C34A8F       ; B
         dw C34A51       ; A..
         dw C34A56       ; B..
         dw C34A5B       ; C..
C34A22:  dw C34A60       ; «========»
         dw C34A6D       ; «========»
         dw C34A7A       ; «========»

; Positioned text for Config page 1
C3490B:  dw $3D8F : db "Controller",$00
C34918:  dw $39B5 : db "Wait",$00
C3491F:  dw $3A65 : db "Fast",$00
C34926:  dw $3A75 : db "Slow",$00
C3492D:  dw $3B35 : db "Short",$00
C34935:  dw $3BA5 : db "On",$00
C3493A:  dw $3BB5 : db "Off",$00
C34940:  dw $3C25 : db "Stereo",$00
C34949:  dw $3C35 : db "Mono",$00
C34950:  dw $3CB5 : db "Memory",$00
C34959:  dw $3D25 : db "Optimum",$00
C34963:  dw $3DB5 : db "Multiple",$00
C3496E:  dw $3A25 : db "1 2 3 4 5 6",$00
C3497C:  dw $3AA5 : db "1 2 3 4 5 6",$00
C3498A:  dw $3C8F : db "Cursor",$00

; Positioned text for Config page 1
C349A1:  dw $78F9 : db "Config",$00
C349AA:  dw $398F : db "Bat.Mode",$00
C349B5:  dw $3A0F : db "Bat.Speed",$00
C349C1:  dw $3A8F : db "Msg.Speed",$00
C349CD:  dw $3B0F : db "Cmd.Set",$00
C349D7:  dw $3B8F : db "Gauge",$00
C349DF:  dw $3C0F : db "Sound",$00
C349E7:  dw $3D0F : db "Reequip",$00
C349F1:  dw $39A5 : db "Active",$00
C349FA:  dw $3B25 : db "Window",$00
C34A03:  dw $3CA5 : db "Reset",$00
C34A0B:  dw $3D35 : db "Empty",$00
C34A13:  dw $3DA5 : db "Single",$00

; Positioned text for Config page 2
C34A34:  dw $418F : db "Mag.Order",$00
C34A40:  dw $438F : db "Window",$00
C34A49:  dw $440F : db "Color",$00
C34A51:  dw $4225 : db "A",!2dot,$00
C34A56:  dw $42A5 : db "B",!2dot,$00
C34A5B:  dw $4325 : db "C",!2dot,$00
C34A60:  dw $452D : db "«±±±±±±±±»",$00
C34A6D:  dw $45AD : db "«±±±±±±±±»",$00
C34A7A:  dw $462D : db "«±±±±±±±±»",$00
C34A87:  dw $4525 : db "R",$00
C34A8B:  dw $45A5 : db "G",$00
C34A8F:  dw $4625 : db "B",$00
C34A93:  dw $43A5 : db "1 2 3 4 5 6 7 8",$00
C34AA5:  dw $41A5 : db "1 2 3 4 5 6",$00
C34AB3:  dw $4425 : db "Font",$00
C34ABA:  dw $4435 : db "Window",$00
C34AC9:  dw $44B5 : db !pal1,!pal2,!pal3,!pal4,!pal5,!pal6,!pal7,$00

; Relative pointers to Mag.Order strings
C340D2:  db $00,$08,$10  ; Healing, Attack, Effect
         db $00,$10,$08  ; Healing, Effect, Attack
         db $08,$10,$00  ; Attack, Effect, Healing
         db $08,$00,$10  ; Attack, Healing, Effect
         db $10,$00,$08  ; Effect, Healing, Attack
         db $10,$08,$00  ; Effect, Attack, Healing

; Mag.Order strings
C34AD3:  db !whit,"Healing"
         db !blck,"Attack "
         db !gray,"Effect "

; Text pointers for "Cntlr1"
cnt_b:   dw C34B08       ; Member A
         dw C34B1A       ; Member B
         dw C34B2C       ; Member C
         dw C34B3E       ; Member D

; Text pointers for "Cntlr2"
cnt_c:   dw C34B11       ; Member A
         dw C34B23       ; Member B
         dw C34B35       ; Member C
         dw C34B47       ; Member D

; Positioned text for Cmd.Set menu
C34AF1:  dw $78CF : db "Arrange",$00

; Positioned text for Controller menu
C34AFB:  dw $7B4D : db "Controller",$00
C34B08:  dw $7C21 : db "Cntlr1",$00
C34B11:  dw $7C33 : db "Cntlr2",$00
C34B1A:  dw $7CA1 : db "Cntlr1",$00
C34B23:  dw $7CB3 : db "Cntlr2",$00
C34B2C:  dw $7D21 : db "Cntlr1",$00
C34B35:  dw $7D33 : db "Cntlr2",$00
C34B3E:  dw $7DA1 : db "Cntlr1",$00
C34B47:  dw $7DB3 : db "Cntlr2",$00

; Pointers to skill names for Skills menu
skillnm: dw C35C48       ; Espers
         dw C35C51       ; Magic
         dw C35C59       ; SwdTech
         dw C35C63       ; Blitz
         dw C35C6B       ; Lore
         dw C35C72       ; Rage
         dw C35C79       ; Dance

; Positioned text for Skills menu
C35C48:  dw $790D : db "Espers",$00
C35C51:  dw $798D : db "Magic",$00
C35C59:  dw $7A8D : db "SwdTech",$00
C35C63:  dw $7B0D : db "Blitz",$00
C35C6B:  dw $7B8D : db "Lore",$00
C35C72:  dw $7C0D : db "Rage",$00
C35C79:  dw $7C8D : db "Dance",$00

; Text pointers for Skills menu
C35C81:  dw C35CC2       ; LV
         dw C35CC7       ; HP
         dw C35CCC       ; MP
C35CD_:  dw C35CD1       ; / (HP)
         dw C35CD5       ; / (MP)

; Positioned text for Skills submenus
C35C87:  dw $81B7 : db "MP…    ",$00
C35C91:  dw $81B7 : db "Lore",$00
C35C98:  dw $81B7 : db "Rage",$00
C35C9F:  dw $81B7 : db "Dance",$00
C35CA7:  dw $81B7 : db "Espers",$00
C35CB0:  dw $81B7 : db "Blitz",$00
C35CB8:  dw $81B7 : db "SwdTech",$00
C35CC2:  dw $422D : db "LV",$00
C35CC7:  dw $42AD : db "HP",$00
C35CCC:  dw $432D : db "MP",$00
C35CD1:  dw $42BB : db "/",$00
C35CD5:  dw $433B : db "/",$00
C35CD9:             db " has it!",$00
C35CE2:  dw $4439 : db "Skill",$00
C35CEA:  dw $4423 : db "Learn.Rate",$00
C35CF7:  dw $4713 : db "At level up...",$00

; Tile attributes for Blitz inputs
C35C1C:  dw $0000        ; Terminator
         dw $0000        ; Bogus; for A
         dw $0000        ; Bogus; for B
         dw $2097        ; X
         dw $2098        ; Y
         dw $208B        ; L
         dw $2091        ; R
         dw $20D6        ; Down-left
         dw $A0D4        ; Down
         dw $60D6        ; Down-right
         dw $20D5        ; Right
         dw $C0D6        ; Up-right
         dw $20D4        ; Up
         dw $A0D6        ; Up-left
         dw $60D5        ; Left

; Text pointers for spell usage menu
C3588_:  dw C35889       ; MP
         dw C3588E       ; Needed

; Positioned text, "MP Needed"
C35889:  dw $7A15 : db "MP",$00
C3588E:  dw $7A4D : db "Needed",$00

; Text pointers for Status menu
C36437:  dw C36497       ; Vigor
         dw C3649F       ; Stamina
         dw C364A9       ; Mag.Pwr
         dw C364B3       ; Evade %
         dw C364BD       ; MBlock%
         dw C364C7       ; ..
         dw C364CB       ; ..
         dw C364CF       ; ..
         dw C364D3       ; ..
         dw C364D7       ; ..
         dw C364DB       ; ..
         dw C364DF       ; ..
         dw C364E3       ; ..
         dw C364E7       ; ..
         dw C3646F       ; Status
         dw C364EB       ; Speed
         dw C364F3       ; Bat.Pwr
         dw C364FD       ; Defense
         dw C36507       ; Mag.Def
         dw C36511       ; Your Exp:
         dw C3651D       ; For level up:

; Text pointers for Status menu
C36455:  dw C36488       ; LV
         dw C3648D       ; HP
         dw C36492       ; MP
C3645B:  dw C36478       ; / (HP)
         dw C3647C       ; / (MP)
         dw C36480       ; % (Evade)
         dw C36484       ; % (MBlock)

; Positioned text for Status menu
C3646F:  dw $78CD : db "Status",$00
C36478:  dw $3A6B : db "/",$00
C3647C:  dw $3AAB : db "/",$00
C36480:  dw $7F83 : db "%",$00
C36484:  dw $8883 : db "%",$00
C36488:  dw $3A1D : db "LV",$00
C3648D:  dw $3A5D : db "HP",$00
C36492:  dw $3A9D : db "MP",$00
C36497:  dw $7ECF : db "Vigor",$00
C3649F:  dw $7FCF : db "Stamina",$00
C364A9:  dw $884F : db "Mag.Pwr",$00
C364B3:  dw $7F69 : db "Evade %",$00
C364BD:  dw $8869 : db "MBlock%",$00
C364C7:  dw $7EDD : db !2dot,$00
C364CB:  dw $7F5D : db !2dot,$00
C364CF:  dw $7FDD : db !2dot,$00
C364D3:  dw $885D : db !2dot,$00
C364D7:  dw $7EFB : db !2dot,$00
C364DB:  dw $7F7B : db !2dot,$00
C364DF:  dw $7E7B : db !2dot,$00
C364E3:  dw $7FFB : db !2dot,$00
C364E7:  dw $887B : db !2dot,$00
C364EB:  dw $7F4F : db "Speed",$00
C364F3:  dw $7E69 : db "Bat.Pwr",$00
C364FD:  dw $7EE9 : db "Defense",$00
C36507:  dw $7FE9 : db "Mag.Def",$00
C36511:  dw $7C4D : db "Your Exp:",$00
C3651D:  dw $7D4D : db "For level up:",$00

; Positioned text for actor naming menu
C368E3:  dw $411B : db "Please enter a name.", $00

; Text pointers for Lineup menu
C379C9:  dw C379CF       ; LV
         dw C379D4       ; HP
         dw C379D9       ; MP
C379D_:  dw C379DE       ; / (HP)
         dw C379E2       ; / (MP)

; Positioned text for Lineup menu
C379CF:  dw $3A6D : db "LV",$00
C379D4:  dw $3AED : db "HP",$00
C379D9:  dw $3B6D : db "MP",$00
C379DE:  dw $3AFB : db "/",$00
C379E2:  dw $3B7B : db "/",$00

; Positioned text for Lineup menu
C37AAE:  dw $390D : db "Lineup",$00
C37A95:  dw $391D : db "Form   group(s).    ",$00
C37AB7:  dw $391D : db "You need   group(s)!",$00
C37ACE:  dw $391D : db "No one there!       ",$00

; Text pointers for Item menu
C38D10:  dw C38D1D       ; USE
         dw C38D23       ; ARRANGE
         dw C38D2D       ; RARE

; Positioned text for Item menu
C38D16:  dw $790D : db "Item",$00
C38D1D:  dw $791D : db "USE",$00
C38D23:  dw $7927 : db "ARRANGE",$00
C38D2D:  dw $7939 : db "RARE",$00

; Text pointers for gear data menu
C38D45:  dw C38D77       ; Vigor
         dw C38DCB       ; Speed
         dw C38D7F       ; Stamina
         dw C38D89       ; Mag.Pwr
         dw C38DD3       ; Bat.Pwr
         dw C38DDD       ; Defense
         dw C38D93       ; Evade %
         dw C38DE7       ; Mag.Def
         dw C38D9D       ; MBlock%
         dw C38DA7       ; ..
         dw C38DAB       ; ..
         dw C38DAF       ; ..
         dw C38DB3       ; ..
         dw C38DB7       ; ..
         dw C38DBB       ; ..
         dw C38DBF       ; ..
         dw C38DC3       ; ..
         dw C38DC7       ; ..
C38D69:  dw C38DF1       ; 50% Dmg
         dw C38DFB       ; Absorb HP
         dw C38E07       ; No Effect
         dw C38E13       ; Weak pt

; Positioned text for gear data menu
C38D71:  dw $8643 : db "???",$00
C38D77:  dw $842F : db "Vigor",$00
C38D7F:  dw $852F : db "Stamina",$00
C38D89:  dw $85AF : db "Mag.Pwr",$00
C38D93:  dw $872F : db "Evade %",$00
C38D9D:  dw $882F : db "MBlock%",$00
C38DA7:  dw $843F : db !2dot,$00
C38DAB:  dw $84BF : db !2dot,$00
C38DAF:  dw $853F : db !2dot,$00
C38DB3:  dw $85BF : db !2dot,$00
C38DB7:  dw $863F : db !2dot,$00
C38DBB:  dw $86BF : db !2dot,$00
C38DBF:  dw $873F : db !2dot,$00
C38DC3:  dw $87BF : db !2dot,$00
C38DC7:  dw $883F : db !2dot,$00
C38DCB:  dw $84AF : db "Speed",$00
C38DD3:  dw $862F : db "Bat.Pwr",$00
C38DDD:  dw $86AF : db "Defense",$00
C38DE7:  dw $87AF : db "Mag.Def",$00
C38DF1:  dw $7B8D : db "50% Dmg",$00
C38DFB:  dw $7BA9 : db "Absorb HP",$00
C38E07:  dw $7C8D : db "No Effect",$00
C38E13:  dw $7CA9 : db "Weak pt",$00
C38E1D:  dw $7B8D : db "Attack",$00
C38E26:  dw $822F : db "SwdTech",$00
C38E30:  dw $82AF : db "Runic",$00
C38E38:  dw $832F : db "2-hand",$00

; Positioned text for item usage menu
C38E41:  dw $7A0D : db "Owned:",$00

; Text for gear data menu
C38D34:  db " can be used by:",$00

; Positioned text for blanking item count in Item menu
C38E4A:  dw $7ABF : db "   ",$00

; Text for evasion modifiers
C38854:  db "  0",$00
         db "+10",$00
         db "+20",$00
         db "+30",$00
         db "+40",$00
         db "+50",$00
         db "-10",$00
         db "-20",$00
         db "-30",$00
         db "-40",$00
         db "-50",$00

; Text for non-evasion stat modifiers
C38880:  db " 0"
         db "+1"
         db "+2"
         db "+3"
         db "+4"
         db "+5"
         db "+6"
         db "+7"
         db " 0"         ; Unused
         db "-1"
         db "-2"
         db "-3"
         db "-4"
         db "-5"
         db "-6"
         db "-7"

; Text pointers for Equip menu
C3A2A6:  dw C3A31A       ; EQUIP
         dw C3A322       ; OPTIMUM
         dw C3A32C       ; RMOVE
         dw C3A334       ; EMPTY
C3A2AE:  dw C3A2CC       ; Head
         dw C3A2D3       ; Body

; Text pointers for Relic menu
C3A2B2:  dw C3A33C       ; EQUIP
         dw C3A344       ; REMOVE
C3A2B6:  dw C3A2DA       ; Relic
         dw C3A2E2       ; Relic

; Positioned text for Equip and Relic menus
C3A2BA:  dw $7A0D : db "R-hand",$00
C3A2C3:  dw $7A8D : db "L-hand",$00
C3A2CC:  dw $7B0D : db "Head",$00
C3A2D3:  dw $7B8D : db "Body",$00
C3A2DA:  dw $7B0D : db "Relic",$00
C3A2E2:  dw $7B8D : db "Relic",$00

; Positioned text for title in Equip and Relic menus
C3A309:  dw $7939 : db "EQUIP",$00
C3A311:  dw $7939 : db "REMOVE",$00

; Positioned text for options in Equip menu
C3A31A:  dw $790D : db "EQUIP",$00
C3A322:  dw $791B : db "OPTIMUM",$00
C3A32C:  dw $792D : db "RMOVE",$00
C3A334:  dw $793B : db "EMPTY",$00

; Positioned text for options in Relic menu
C3A33C:  dw $7911 : db "EQUIP",$00
C3A344:  dw $791F : db "REMOVE",$00

; Text pointers for Equip and Relic menus
C3A34D:  dw C3A371       ; Vigor
         dw C3A3C5       ; Speed
         dw C3A379       ; Stamina
         dw C3A383       ; Mag.Pwr
         dw C3A3CD       ; Bat.Pwr
         dw C3A3D7       ; Defense
         dw C3A38D       ; Evade %
         dw C3A3E1       ; Mag.Def
         dw C3A397       ; MBlock%
         dw C3A3A1       ; ->
         dw C3A3A5       ; ->
         dw C3A3A9       ; ->
         dw C3A3AD       ; ->
         dw C3A3B1       ; ->
         dw C3A3B5       ; ->
         dw C3A3B9       ; ->
         dw C3A3BD       ; ->
         dw C3A3C1       ; ->

; Positioned text for Equip and Relic menus
C3A371:  dw $7CA9 : db "Vigor",$00
C3A379:  dw $7DA9 : db "Stamina",$00
C3A383:  dw $7E29 : db "Mag.Pwr",$00
C3A38D:  dw $7FA9 : db "Evade %",$00
C3A397:  dw $80A9 : db "MBlock%",$00
C3A3A1:  dw $7CBD : db ">",$00
C3A3A5:  dw $7D3D : db ">",$00
C3A3A9:  dw $7DBD : db ">",$00
C3A3AD:  dw $7E3D : db ">",$00
C3A3B1:  dw $7F3D : db ">",$00
C3A3B5:  dw $7FBD : db ">",$00
C3A3B9:  dw $7EBD : db ">",$00
C3A3BD:  dw $803D : db ">",$00
C3A3C1:  dw $80BD : db ">",$00
C3A3C5:  dw $7D29 : db "Speed",$00
C3A3CD:  dw $7EA9 : db "Bat.Pwr",$00
C3A3D7:  dw $7F29 : db "Defense",$00
C3A3E1:  dw $8029 : db "Mag.Def",$00
C3A3EB:  dw $7923 : db "Empty",$00
C3A3F3:  dw $7921 : db "Optimum",$00
C3A3FD:  dw $79A3 : db "Empty",$00
C3A405:  dw $79A1 : db "Optimum",$00
C3A40F:  dw $7A15 : db "Equipment changed.",$00

; Text pointers for Optimum message
reeqptr: dw C3A3F3       ; Optimum (high)
         dw C3A405       ; Optimum (low)
         dw C3A3EB       ; Empty (high)
         dw C3A3FD       ; Empty (low)

; Text pointers for final battle lineup
C3AC84:  dw C3AC8A       ; End
         dw C3AC90       ; Reset
         dw C3AC98       ; Determine order

; Positioned text for final battle lineup
C3AC8A:  dw $4011 : db "End",$00
C3AC90:  dw $3991 : db "Reset",$00
C3AC98:  dw $391D : db "Determine order",$00

; Positioned text for Colosseum item menu
C3AD9A:  dw $790D : db "Colosseum",$00
C3ADA6:  dw $7923 : db "Select an Item",$00

; Positioned text for Colosseum matchup
C3B40F:  dw $7D15 : db "Select the challenger",$00
C3B427:  dw $790D : db "?????????????",$00

; Pointers to shop titles
C3C00C:  dw C3C2FC       ; Weapon
         dw C3C305       ; Armor
         dw C3C30D       ; Item
         dw C3C314       ; Relics
         dw C3C31D       ; Vendor

; Positioned text for shop menu
C3C2FC:  dw $790D : db "Weapon",$00
C3C305:  dw $790F : db "Armor",$00
C3C30D:  dw $790F : db "Item",$00
C3C314:  dw $790D : db "Relics",$00
C3C31D:  dw $790D : db "Vendor",$00
C3C326:  dw $7A0F : db "BUY  SELL  EXIT",$00
C3C338:  dw $7A41 : db "GP",$00
C3C33D:  dw $7B2B : db "GP",$00
C3C342:  dw $7AB3 : db "Owned:",$00
C3C34B:  dw $7BB3 : db "Equipped:",$00
C3C357:  dw $7B8F : db "Bat Pwr",$00
C3C361:  dw $7B8F : db "Defense",$00
C3C36B:  dw $7BA5 : db "…",$00
C3C36F:  dw $791F : db "Hi! Can I help you?",$00
C3C385:  dw $791F : db "Help yourself!",$00
C3C396:  dw $791F : db "How many?",$00
C3C3A2:  dw $791F : db "Whatcha got?",$00
C3C3BD:  dw $791F : db "Bye!          ",$00
C3C3CE:  dw $791F : db "You need more GP!",$00
C3C3E2:  dw $791F : db "Too many!     ",$00
C3C3F5:  dw $791F : db "One's plenty! ",$00

; =========================[ New general functions ]===========================

; Preserve and then set data bank to 7E
db7e_:   PLY             ; Return address
         PHB             ; Save DB
         PHY             ; Repush address

; Set data bank register to 7E
db7e:    LDA #$7E        ; Bank: 7E
         PHA             ; Put on stack
         PLB             ; Set DB to 7E
         TDC             ; Clear A
         RTS

; Set WRAM address to 7E/9D89
wm9D89:  PHX             ; Save X
         LDX #$9D89      ; 7E/9D89
         BRA wmplx       ; Set WRAM LBs

; Set WRAM address to 7E/9E09
wm9E09:  LDX #$9E09      ; 7E/9E09
         BRA wm_set      ; Set WRAM LBs

; Set WRAM address to 7E/9E8B
wm9E8B:  PHX             ; Save X
         LDX #$9E8B      ; 7E/9E8B
wmplx:   STX $2181       ; Set WRAM LBs
         PLX             ; Restore X
         RTS

; Set WRAM address to 7E/AA8D
wmAA8D:  LDX #$AA8D      ; 7E/AA8D
wm_set:  PHX             ; Prepare PLX
         BRA wmplx       ; Set WRAM LBs

; Multiply A by 16, 32, or 64
x64:     REP #$20        ; 16-bit A
         ASL A           ; x64
x32r:    REP #$20        ; 16-bit A
x32:     ASL A           ; x32
x16:     ASL A           ; x16
         ASL A           ; x8
         ASL A           ; x4
         ASL A           ; x2
         RTS

; Multiply B by A
mul8:    PHP             ; Save PS
         REP #$20        ; 16-bit A
         STA $004202     ; Set factors
         NOP             ; Waste 2 cycles
         NOP             ; Waste 2 cycles
         LDA $004216     ; Product
         PLP             ; Restore PS
         CLC             ; Prepare ADC
         RTS

; Multiply 8-bit A by Y
mul16:   PHA             ; Save multiplicand
.loop    LDA $004212     ; PPU status
         ASL A           ; H-Blank?
         BPL .loop       ; Loop if not
         PLA             ; Multiplicand
         STA $00211C     ; Set matrix B
         PHY             ; Push multiplier
         PLA             ; Pull low byte
         STA $00211B     ; Set matrix A LB
         PLA             ; Pull high byte
         STA $00211B     ; Set matrix A HB
         LDX $2134       ; Product LBs
         LDA $2136       ; Product HB
         RTS             ; ^ Add 00 if needed

; Divide |A| by 16
abs16:   BMI .neg        ; If negative
         JMP C3CC33      ; Modifier / 16
.neg     JSR invert      ; Invert value
         JSR C3CC33      ; Modifier / 16
invert:  EOR #$FFFF      ; Invert value
         INC A           ; Add 1
         RTS

; Load and index $28
ld28:    TDC             ; Clear A
         LDA $28         ; Member slot
         TAX             ; Index it
         RTS

; Load, double, and then index $28
ld28x2:  TDC             ; Clear A
         LDA $28         ; Member slot
         ASL A           ; Double it
         TAX             ; Index it
         RTS

; Get uppermost BG1 scanline
topbg1:  TDC             ; Clear A
         LDA $49         ; BG1 top row
         JSR x16         ; x16
         REP #$21        ; 16-bit A; C-
         RTS

; Check if actor is forced into party
forced:  ASL A           ; Actor x2
         TAX             ; Index it
         REP #$20        ; 16-bit A
         LDA $0202       ; Forced actors
         BRA chk1EDY     ; Includes ours?

; Check if actor is eligible for display
chk1EDC: PHX             ; Save X
         JSR charadY     ; Actor's address
         TXY             ; Put in Y
         PLX             ; Restore X
chk1EDX: REP #$20        ; 16-bit A
         LDA $1EDC       ; Showable actors
chk1EDY: AND.L C39C67,X  ; Has our actor?
chk1EDZ: SEP #$20        ; 8-bit A
         RTS

; Check if processing Gogo
gogoch1: JSR C34EDD      ; Actor's address
gogoch2: LDA $0000,Y     ; Actor in slot
         CMP #$0C        ; Gogo?
         RTS

; Get actor's SRAM block address
charadr: ASL A           ; Actor x2
charadX: TAX             ; Index it
charadY: REP #$20        ; 16-bit A
         LDA.L C36969,X  ; Actor's address
         TAX             ; Index it
         BRA chk1EDZ     ; 8-bit A; exit

; Check if Up button is pressed
key_up:  LDA $0B         ; Semi-auto keys
         BIT #$08        ; Pushing up?
         RTS

; Get active wallpaper
getskin: LDA $1D4E       ; Game options
         AND #$0F        ; Get wallpaper
         RTS

; Check if Cursor Memory is enabled
b6_1D4E: LDA $1D4E       ; Game options
         ASL A           ; Cursor memory?
         RTS

; Get color to view in Config menu
skncol:  LDA $1D54       ; Game options
         AND #$38        ; Get viewed color
         RTS

; Get list size
ld_9D89: LDA $7E9D89     ; List size
         RTS

; Define or clear list slot
st_9D8_: LDA #$FF        ; Null value
st_9D89: STA $7E9D89,X   ; Add to list
         RTS

; Position 64x32 BG1 tilemap at $0000 in VRAM
bg1_2x1: SEP #$20        ; 8-bit A
         LDA #$01        ; 64x32 at $0000
bg1_set: STA $2107       ; Set BG1 map loc
         RTS

; Position 32x64 BG1 tilemap at $0000 in VRAM
bg1_1x2: LDA #$02        ; 64x32 at $0000
         BRA bg1_set     ; Set BG1 map loc

; Load actor's graphics set number
gfxset1: JSR charadr     ; Actor's address
gfxset2: TDC             ; Clear A
         SEP #$20        ; 8-bit A
         LDA $14,X       ; Status
         AND #$20        ; Imp?
         BEQ .nonimp     ; Branch if not
         LDA #$0F        ; Use Imp set
         BRA .exit       ; Skip a line
.nonimp  LDA $01,X       ; Graphics set
.exit    CMP #$0F        ; Set Z if Imp
         RTS

; Set queue's animation table address, etc.
initob0: JSR C3EA6C      ; Follow BG1: Y
initob1: LDA #$C3        ; Bank: C3
initob2: STA $35CA,X     ; Set ptr HB
initob3: JSR setaniX     ; Set ptr LBs
;        INC $3649,X     ; Mode +1
         JMP C3120_      ; Set pose timer

; Set queue's animation table address
setani_: SEP #$20        ; 8-bit A
         LDA #$C3        ; Bank: C3
setanim: JSR C3D23A      ; Set ptr HB
setaniX: REP #$20        ; 16-bit A
         TYA             ; Anim ptr LBs
setaniY: STA $7E32C9,X   ; Put in queue
setaniZ: bra chk1EDZ     ; 8-bit A; exit

; Load M7 animation flags or object pose pointer
ld36C_:  JSR C31227      ; Frame timer -1
ld36C9:  TDC             ; Clear A
         LDA $36C9,X     ; Frame index
         TAY             ; Index it
         REP #$20        ; 16-bit A
         LDA [$EB],Y     ; Flags or ptr
         TAY             ; Put in Y
         BRA setaniZ     ; 8-bit A; exit

; Link animation table pointer to bank D8
D8_35CA: LDA #$D8        ; Bank: D8
         STA $35CA,Y     ; Set ptr HB
         RTS

; Set address to animation frame data
E7_35CA: LDA $35CA,X     ; Anim ptr HB
E7_35C_: JSR C3D51D      ; Define source
         LDY $00         ; Src index: 0
         RTS

; Save cursor position from previous menu
sv_cur:  LDY $4F         ; Cursor position
         STY $8E         ; Set return loc
         LDA $4A         ; Scroll position
         STA $90         ; Set return loc
         RTS

; Restore cursor position from previous menu
oldcur:  LDY $8E         ; Old cursor loc
         STY $4F         ; Set as current
         STY $4D         ; Set onscreen col
         LDA $90         ; Old scroll pos
         STA $4A         ; Set as current
         LDA $50         ; List row
         SEC             ; Prepare SBC
         SBC $4A         ; Deduct scroll pos
         STA $4E         ; Set onscreen row
         RTS

; Clear BG2 tilemaps
bg2c_cl: JSR C36A32      ; Clear BG2 map C
bg2b_cl: JSR C36A2D      ; Clear BG2 map B
         JMP C36A28      ; Clear BG2 map A

; Clear BG3 tilemaps
bg3d_cl: JSR C36A4B      ; Clear BG3 map D
bg3c_cl: JSR C36A46      ; Clear BG3 map C
bg3b_cl: JSR C36A41      ; Clear BG3 map B
         JMP C36A3C      ; Clear BG3 map A

; Replace item in actor's gear slot
unequip: CMP #$FF        ; Empty gear slot?
         BEQ .skip       ; Branch if so
         JSR C39D5E      ; Put in stock
.skip    JSR C30EB2      ; Sound: Click
invitm_: JSR C3BFC2      ; X = Gear list slot
invitem: LDA $7E9D8A,X   ; Inventory slot
         TAX             ; Index it
         JMP C38C2F      ; Selected item

; Shift BG1 write row down by 4 rows
e6add4:  JSR e6add2      ; BG1 WR row +2

; Shift BG1 write row down by 2 rows
e6add2:  LDA $E6         ; BG1 write row
         INC A           ; Go 1 row down
         INC A           ; Go 1 row down
         AND #$1F        ; Stay in limits
         STA $E6         ; Save changes
         RTS

; Get skill in menu slot, set text position
sk_slt1: LDA $E6         ; BG1 write row
         INC A           ; Go 1 row down
         BRA sk_slt3     ; Skip a line
sk_slt2: LDA $E6         ; BG1 write row
sk_slt3: JSR C3809F      ; Compute map ptr
         JSR C3BAB3      ; Set position
sk_slt4: TDC             ; Clear A
         LDA $E5         ; Skill slot
sk_slt5: TAX             ; Index it
sk_slt6: LDA $7E9D89,X   ; Skill in slot
         RTS

; Get item's class
itmtype: JSR C38321      ; Compute index
itmtyp_: LDA $D85000,X   ; Properties
         AND #$07        ; Get class
         CMP $E0         ; Sought type?
         RTS

; Get item's properties
itm_pro: JSR C38321      ; Compute index
itm_pr_: LDA $D85013,X   ; Properties
         ASL A           ; Shift bits
         RTS

; Get item's power
itm_pwr: JSR C38321      ; Compute index
itm_pw_: LDA $D85014,X   ; Item's power
         RTS

; Get item's base price
itm_prc: JSR C38321      ; Compute index
         REP #$20        ; 16-bit A
         LDA $D8501C,X   ; Base price
         RTS

; Add 8-bit number's digits to string
add3n:   LDA $F7         ; Space char
add3n_:  STA $2180       ; Add to string
add2n:   LDA $F8         ; Tens digit
         STA $2180       ; Add to string
         LDA $F9         ; Ones digit
         JMP C3C13D      ; Add to string

; Get actor by cursor in Lineup menu
lnp_cur: TDC             ; Clear A
         LDA $4B         ; Cursor slot
         ORA $4A         ; +16 if on party
         BRA sk_slt5     ; Actor in slot

; Get group count for Lineup menu
ptycnt:  LDA $0201       ; Group count
         AND #$07        ; Clear flag
         DEC A           ; Now 0-6
         RTS

; Draw multiple menu windows
C3034Y:  PHX             ; Save index
         JSR C30341      ; Draw a window
         PLX             ; Window index
         INX             ; Index +1
         INX             ; Index +1
         INX             ; Index +1
         INX             ; Index +1
         LDA.L C30000,X  ; More windows?
         BNE C3034Y      ; Loop if so
         RTS

; =============================================================================

; Warn on bank overflow
warnpc $C40000

; Blank rest of bank
padbyte $FF : pad $C40000
